結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-03-23 22:41:58 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 186 ms / 4,000 ms |
| コード長 | 2,015 bytes |
| コンパイル時間 | 1,308 ms |
| コンパイル使用メモリ | 98,196 KB |
| 実行使用メモリ | 23,124 KB |
| 最終ジャッジ日時 | 2024-11-23 19:20:10 |
| 合計ジャッジ時間 | 4,279 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using i64 = int64_t;
struct edge { int to; i64 cost; };
constexpr i64 inf = 987'654'321'987'654'321LL;
template <class T>
inline void readint(T *x) {
*x = 0;
int c;
for(;;) {
c = getchar_unlocked();
switch(c) case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: goto num;
}
num:
for(;;) {
switch(c) {
case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
*x *= 10;
*x += c - 48;
break;
default:
return;
}
c = getchar_unlocked();
}
}
int main(void) {
int N, M; readint(&N); readint(&M);
vector<vector<edge>> G(N, vector<edge>());
for(int i=0; i<M; ++i) {
int a, b, c; readint(&a); readint(&b); readint(&c);
--a, --b;
G[a].push_back(edge{b, c});
G[b].push_back(edge{a, c});
}
// cost[チケットを使ったか][頂点] := 最小コスト
vector<vector<i64>> cost(2, vector<i64>(N, inf));
cost[0][0] = 0;
cost[1][0] = 0;
vector<vector<bool>> seen(2, vector<bool>(N, false));
// コスト、頂点、チケットを使ったか
priority_queue<tuple<i64, int, bool>, vector<tuple<i64, int, bool>>, greater<tuple<i64, int, bool>>> pq;
pq.emplace(0, 0, false);
while(!pq.empty()) {
i64 _; int v; bool used; tie(_, v, used) = pq.top(); pq.pop();
if(seen[used][v]) { continue; }
seen[used][v] = true;
for(edge &e : G[v]) {
// チケットを使わない
if(cost[used][e.to] > cost[used][v] + e.cost) {
cost[used][e.to] = cost[used][v] + e.cost;
pq.emplace(cost[used][e.to], e.to, used);
}
// チケットを使う
if(!used && cost[true][e.to] > cost[false][v]) {
cost[true][e.to] = cost[false][v];
pq.emplace(cost[true][e.to], e.to, true);
}
}
}
for(int v=0; v<N; ++v) {
printf("%ld\n", cost[false][v] + cost[true][v]);
}
return 0;
}