結果

問題 No.2565 はじめてのおつかい
ユーザー 4O44O4
提出日時 2023-12-02 16:09:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 2,395 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 215,788 KB
実行使用メモリ 16,628 KB
最終ジャッジ日時 2023-12-02 16:09:39
合計ジャッジ時間 6,845 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 115 ms
16,628 KB
testcase_04 AC 87 ms
16,628 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 64 ms
8,292 KB
testcase_07 AC 32 ms
6,548 KB
testcase_08 AC 34 ms
6,548 KB
testcase_09 AC 61 ms
7,776 KB
testcase_10 AC 39 ms
7,012 KB
testcase_11 AC 91 ms
12,536 KB
testcase_12 AC 20 ms
6,548 KB
testcase_13 AC 40 ms
6,764 KB
testcase_14 AC 67 ms
9,664 KB
testcase_15 AC 70 ms
9,316 KB
testcase_16 AC 69 ms
14,020 KB
testcase_17 AC 16 ms
6,548 KB
testcase_18 AC 21 ms
7,344 KB
testcase_19 AC 82 ms
11,524 KB
testcase_20 AC 76 ms
10,532 KB
testcase_21 AC 68 ms
10,552 KB
testcase_22 AC 37 ms
8,024 KB
testcase_23 AC 47 ms
8,196 KB
testcase_24 AC 8 ms
6,548 KB
testcase_25 AC 75 ms
10,812 KB
testcase_26 AC 45 ms
7,876 KB
testcase_27 AC 69 ms
11,200 KB
testcase_28 AC 92 ms
11,276 KB
testcase_29 AC 96 ms
13,144 KB
testcase_30 AC 76 ms
11,888 KB
testcase_31 AC 74 ms
10,896 KB
testcase_32 AC 42 ms
7,812 KB
testcase_33 AC 73 ms
11,600 KB
testcase_34 AC 78 ms
10,376 KB
testcase_35 AC 72 ms
12,908 KB
testcase_36 AC 78 ms
11,540 KB
testcase_37 AC 43 ms
7,924 KB
testcase_38 AC 74 ms
10,464 KB
testcase_39 AC 64 ms
9,056 KB
testcase_40 AC 85 ms
12,964 KB
testcase_41 AC 97 ms
13,628 KB
testcase_42 AC 45 ms
7,576 KB
testcase_43 AC 73 ms
9,856 KB
testcase_44 AC 36 ms
6,564 KB
testcase_45 AC 19 ms
6,548 KB
testcase_46 AC 77 ms
9,648 KB
testcase_47 AC 47 ms
7,080 KB
testcase_48 AC 43 ms
6,820 KB
testcase_49 AC 19 ms
6,548 KB
testcase_50 AC 50 ms
7,780 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 41 ms
7,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
int ctoi(char c){return c-'0';}
ll ctoll(char c){return c-'0';}

const long long INF = 1LL << 60;

//辺を表す型
struct Edge{
     int to; //隣接頂点番号
     long long w; //重み
     Edge(int to, long long w) : to(to), w(w) {}
};

//重み付きグラフを表す型
using Graph = vector<vector<Edge>>;

//緩和を実施する関数
template<class T> bool chmin(T& a, T b){
     if(a > b){
          a = b;
          return true;
     }
     else return false;
}

vector<long long> dijkstra(Graph G, int s){
     int N = G.size();
     //ダイクストラ法
     vector<bool>used(N,false);
     vector<long long>dist(N, INF);
     dist[s] = 0;
     
     priority_queue<pair<long long, int>,vector<pair<long long, int>>, greater<pair<long long, int>>> que;
     que.push(make_pair(dist[s], s));
     
     while(!que.empty()){
          //v:使用済みでない頂点のうち,d[v]が最小の頂点
          //d:vに対するキー値
          int v = que.top().second;
          long long d = que.top().first;
          que.pop();

          if(d > dist[v])continue;

          for(auto e : G[v]){
               if(chmin(dist[e.to],dist[v]+e.w)){
                    que.push(make_pair(dist[e.to], e.to));
               }
          }
     }

     return dist;
}

int main(){
     //長点数, 辺数, 始点
     int N,M;
     cin >> N >> M;
     int s = 0;

     Graph G(N);

     //辺を追加
     for(int i = 0;i < M;i++){
          int a,b;
          cin >> a >> b;
          a--;b--;
          G[a].push_back(Edge(b,1));
     }

     //グラフと始点を渡すと始点からの各頂点への最短距離が帰ってくる
     vector<ll>dist1 = dijkstra(G,0);
     vector<ll>dist2 = dijkstra(G,N-1);
     vector<ll>dist3 = dijkstra(G,N-2);

     ll cost1 = INF;
     ll cost2 = INF;
     ll cost3 = INF;
     ll cost4 = INF;

     cost1 = dist1[N-1] + dist2[N-2] + dist3[0];
     cost2 = dist1[N-2] + dist3[N-1] + dist2[0];
     
     if(dist1[N-1] == dist1[N-2]+dist2[N-2]){
          cost3 = dist1[N-1] + dist2[0];
     }
     if(dist1[N-2] == dist1[N-1]+dist3[N-1]){
          cost4 = dist1[N-2] + dist3[0];
     }

     ll ans = min(cost1,min(cost2,min(cost3,cost4)));
     if(ans == INF)cout << -1 << endl;
     else cout << ans << endl;
}
0