結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-03-23 14:55:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 225 ms / 4,000 ms |
| コード長 | 1,983 bytes |
| コンパイル時間 | 956 ms |
| コンパイル使用メモリ | 95,048 KB |
| 最終ジャッジ日時 | 2025-01-07 00:17:45 |
|
ジャッジサーバーID (参考情報) |
judge1 / 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'321;
template <class T>
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(N, vector<i64>(2, inf));
cost[0][0] = 0;
cost[0][1] = 0;
vector<vector<bool>> seen(N, vector<bool>(2, 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()) {
auto [_, v, used] = pq.top(); pq.pop();
if(seen[v][used]) { continue; }
seen[v][used] = true;
for(edge &e : G[v]) {
// チケットを使わない
if(cost[e.to][used] > cost[v][used] + e.cost) {
cost[e.to][used] = cost[v][used] + e.cost;
pq.emplace(cost[e.to][used], e.to, used);
}
// チケットを使う
if(!used && cost[e.to][true] > cost[v][false]) {
cost[e.to][true] = cost[v][false];
pq.emplace(cost[e.to][true], e.to, true);
}
}
}
for(int v=0; v<N; ++v) {
printf("%ld\n", cost[v][true] + cost[v][false]);
}
return 0;
}