結果
問題 |
No.614 壊れたキャンパス
|
ユーザー |
|
提出日時 | 2017-12-26 00:00:08 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 963 ms / 2,000 ms |
コード長 | 2,145 bytes |
コンパイル時間 | 1,247 ms |
コンパイル使用メモリ | 104,092 KB |
実行使用メモリ | 78,340 KB |
最終ジャッジ日時 | 2024-12-17 20:24:11 |
合計ジャッジ時間 | 11,125 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:12:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 12 | int n, m, k, s, t; scanf("%d%d%d%d%d", &n, &m, &k, &s, &t); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:23:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 23 | int a, b, c; scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <map> #include <queue> using namespace std; using i64 = long long; struct edge { int to; i64 cost; }; int main(void) { constexpr i64 inf = 987654321987654321LL; int n, m, k, s, t; scanf("%d%d%d%d%d", &n, &m, &k, &s, &t); --s, --t; int N = 0; // 頂点数 vector<map<int, int>> mp(n); // 座標圧縮用データ。 mp[a][b] := a棟b階についた連番 vector<vector<int>> route(n); // route[a] := a棟からa+1棟へ廊下がある階のリスト vector<tuple<int, int, int>> dat; // 入力で与えられる m 個の渡り廊下のリスト mp[0] [s] = N++; mp[n-1][t] = N++; route[0] .push_back(s); route[n-1].push_back(t); for(int i=0; i<m; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); --a, --b, --c; dat.emplace_back(a, b, c); if(!mp[a].count(b)) { mp[a][b] = N++; route[a].push_back(b); } if(!mp[a+1].count(c)) { mp[a+1][c] = N++; route[a+1].push_back(c); } } vector<vector<edge>> G(N); for(int i=0; i<m; ++i) { int a, b, c; tie(a, b, c) = dat[i]; G[mp[a][b]].push_back(edge{mp[a+1][c], 0}); } for(int a=0; a<n; ++a) { sort(begin(route[a]), end(route[a])); for(int k=0, size=route[a].size(); k<size-1; ++k) { int b0 = route[a][k], b1 = route[a][k+1]; if(b0 == b1) { continue; } int u = mp[a][b0], v = mp[a][b1]; G[u].push_back(edge{v, b1-b0}); G[v].push_back(edge{u, b1-b0}); } } // ここからダイクストラ vector<i64> cost(N, inf); vector<bool> seen(N, false); cost[mp[0][s]] = 0; priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq; // コスト、点番号 pq.emplace(0, mp[0][s]); while(!pq.empty()) { i64 _; int v; tie(_, v) = pq.top(); pq.pop(); if(seen[v]) { continue; } seen[v] = true; for(edge e : G[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[mp[n-1][t]]; if(res == inf) { res = -1; } printf("%lld\n", res); return 0; }