結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-22 02:39:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 424 ms / 4,000 ms |
| コード長 | 2,022 bytes |
| コンパイル時間 | 2,347 ms |
| コンパイル使用メモリ | 182,096 KB |
| 実行使用メモリ | 24,288 KB |
| 最終ジャッジ日時 | 2024-11-23 19:39:18 |
| 合計ジャッジ時間 | 7,582 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct State {
int at;
ll cost;
int used;
int prev;
State(int at, ll cost, int prev) : at(at), cost(cost), used(false), prev(prev) {}
State(int at, ll cost, int used, int prev) : at(at), cost(cost), used(used), prev(prev) {}
bool operator>(const State& s) const {
return cost > s.cost;
}
};
struct Edge {
int to;
ll cost;
Edge(int to, ll cost) : to(to), cost(cost) {}
};
typedef vector<vector<Edge> > AdjList; //隣接リスト
const ll INF = 1e18;
const int NONE = -1;
AdjList graph;
//sは始点、mincは最短経路のコスト、Prevは最短経路をたどる際の前の頂点
void dijkstra(int s, vector< vector<ll> >& minc){
priority_queue<State, vector<State>, greater<State> > pq;
pq.push(State(s, 0, NONE));
minc[0][s] = 0;
while(!pq.empty()) {
State cur = pq.top();
pq.pop();
if (minc[cur.used][cur.at] < cur.cost) continue;
for(Edge e : graph[cur.at]) {
ll cost = cur.cost + e.cost;
if (minc[cur.used][e.to] <= cost) continue;
minc[cur.used][e.to] = cost;
pq.emplace(e.to, cost, cur.used, cur.at);
if (!cur.used) {
cost = cur.cost;
if (minc[1][e.to] <= cost) continue;
minc[1][e.to] = cost;
pq.emplace(e.to, cost, true, cur.at);
}
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
graph.resize(n);
for (int i = 0; i < m; i++) {
int a, b;
ll c;
cin >> a >> b >> c;
a--; b--;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
vector< vector<ll> > minc(2, vector<ll>(n, INF));
dijkstra(0, minc);
for (int i = 0; i < n; i++) {
minc[1][i] = min(minc[1][i], minc[0][i]);
cout << minc[0][i] + minc[1][i] << endl;
}
return 0;
}