結果
問題 |
No.614 壊れたキャンパス
|
ユーザー |
|
提出日時 | 2017-12-14 19:12:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,773 bytes |
コンパイル時間 | 1,247 ms |
コンパイル使用メモリ | 100,960 KB |
最終ジャッジ日時 | 2025-01-05 05:26:40 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 MLE * 2 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:13:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 13 | int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:28:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 28 | int a, b, c; scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <vector> using namespace std; using i64 = long long; constexpr i64 inf = 987654321987654321LL; struct edge { int to, cost; }; int main(void) { int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T); --S, --T; // G[i] := { (i棟の始点の階, i+1棟の終点の階)のリスト } vector<vector<pair<int, int>>> G(N); // vector<pair<int, int>> corr0(M), corr1(M); // corr0[i] := 渡り廊下始点の (棟, 階) // // corr1[i] := 渡り廊下終点の (棟, 階) [未使用] vector<vector<int>> cor0(N); vector<vector<int>> mat0(N), mat1(N); // 始点 // cor0[a][p] = i // mat0[a][p] = b ... a棟 b階は ノードi a棟で p番目 // 終点 // cor1[a+1][p] = i // mat1[a+1][p] = b ... a棟 b階は ノードi a棟で p番目 for(int i=0; i<M; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); --a, --b, --c; G[a].emplace_back(b, c); cor0[a].push_back(i); mat0[a].push_back(b); // cor1[a+1].push_back(i); mat1[a+1].push_back(c); } vector<vector<edge>> H(M+2); // a棟 -> a+1棟 // a棟 b階 -> a+1棟 c階への渡り廊下 // a+1棟 x階 -> a+2棟 y階への渡り廊下 for(int a=0; a<N-1; ++a) { for(int i=0, n=cor0[a].size(); i<n; ++i) { int node0 = cor0[a][i]; // int b = mat0[a][i]; int c = mat1[a+1][i]; for(int j=0, m=cor0[a+1].size(); j<m; ++j) { int node1 = cor0[a+1][j]; int x = mat0[a+1][j]; // int y = mat1[a+2][j]; H[node0].push_back(edge{node1, abs(c - x)}); } } } // 0棟 p階 -> 1棟 q階への渡り廊下 for(int i=0, n=cor0[0].size(); i<n; ++i) { int node1 = cor0[0][i]; int p = mat0[0][i]; H[M].push_back(edge{node1, abs(S - p)}); } // N-2棟 b階 -> N-1棟 c階への渡り廊下 for(int i=0, n=cor0[N-2].size(); i<n; ++i) { int node0 = cor0[N-2][i]; int c = mat1[N-1][i]; H[node0].push_back(edge{M+1, abs(c - T)}); } // コーナーケース if(N == 1) { H[M].push_back(edge{M+1, abs(S - T)}); } vector<i64> cost(M+2, inf); // cost[M+2] cost[M] = 0; priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq; vector<bool> seen(M+2, false); // seen[M+2] // コスト、点番号 pq.emplace(0, M); while(!pq.empty()) { i64 _, v; tie(_, v) = pq.top(); pq.pop(); if(seen[v]) { continue; } seen[v] = true; for(edge e : H[v]) { if(cost[e.to] > cost[v] + e.cost) { cost[e.to] = cost[v] + e.cost; pq.emplace(cost[e.to], e.to); } } } i64 res = cost[M+1]; if(res == inf) { res = -1; } printf("%lld\n", res); return 0; }