結果
問題 |
No.614 壊れたキャンパス
|
ユーザー |
|
提出日時 | 2017-12-14 14:44:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,669 bytes |
コンパイル時間 | 1,257 ms |
コンパイル使用メモリ | 111,508 KB |
最終ジャッジ日時 | 2025-01-05 05:23:14 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 TLE * 2 MLE * 1 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:17:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 17 | scanf("%d%d%d%d%d", &N, &M, &K, &S, &T); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:26:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 26 | scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <unordered_map> #include <queue> using namespace std; using i64 = long long; constexpr i64 inf = 987654321987654321LL; int N, M, K, S, T; int a, b, c; i64 cc; int p, q; i64 _cost; i64 mini; int k; i64 v; int main(void) { scanf("%d%d%d%d%d", &N, &M, &K, &S, &T); --S, --T; priority_queue<tuple<i64, int, int>, vector<tuple<i64, int, int>>, greater<tuple<i64, int, int>>> pq; // コスト, 棟, 階 pq.emplace(0, 0, S); vector<unordered_map<int, i64>> cost(N); // cost[棟][階] := 最小コスト cost[0][S] = 0; vector<vector<pair<int, int>>> G(N, vector<pair<int, int>>()); // G[i] := {(i棟の出次階, i+1棟の入次階)のリスト} for(int i=0; i<M; ++i) { scanf("%d%d%d", &a, &b, &c); --a, --b, --c; G[a].emplace_back(b, c); } if(N > 1 && (G[0].size() == 0 || G[N-2].size() == 0)) { puts("-1"); return 0; } while(!pq.empty()) { // 今 p棟 q階にいる。 tie(cc, p, q) = pq.top(); pq.pop(); if(!cost[p].count(q)) { continue; } _cost = cost[p][q]; if(_cost < cc) { continue; } // 同棟の src 階に移動し、 // 渡り廊下で p+1棟 dst階に移動する for(auto&& pr : G[p]) { int src, dst; tie(src, dst) = pr; if(!cost[p+1].count(dst) || cost[p+1][dst] > _cost + abs(q - src)) { cost[p+1][dst] = _cost + abs(q - src); pq.emplace(cost[p+1][dst], p+1, dst); } } } mini = inf; for(auto&& kv : cost[N-1]) { tie(k, v) = kv; // k階まででコストvかかった mini = min(mini, v + abs(T - k)); } if(mini == inf) { mini = -1; } printf("%lld\n", mini); return 0; }