結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
Bwambocos
|
| 提出日時 | 2019-03-22 22:16:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 632 ms / 4,000 ms |
| コード長 | 2,054 bytes |
| コンパイル時間 | 2,597 ms |
| コンパイル使用メモリ | 192,420 KB |
| 実行使用メモリ | 36,372 KB |
| 最終ジャッジ日時 | 2024-11-23 19:03:16 |
| 合計ジャッジ時間 | 9,782 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge { LL to, cost; };
const LL inf = 112345678901234567;
std::vector<LL>dist1;
std::vector<std::vector<edge>>G;
std::vector<std::vector<LL>>dist2;
void dijkstra1(LL s)
{
std::priority_queue<P, std::vector<P>, std::greater<P>>que;
std::fill(dist1.begin(), dist1.end(), inf);
dist1[s] = 0;
que.push(P(0, s));
while (!que.empty())
{
P p = que.top(); que.pop();
LL v = p.second;
if (dist1[v] < p.first) continue;
rep(i, G[v].size())
{
edge e = G[v][i];
if (dist1[e.to] > dist1[v] + e.cost)
{
dist1[e.to] = dist1[v] + e.cost;
que.push(P(dist1[e.to], e.to));
}
}
}
}
void dijkstra2(LL s)
{
std::priority_queue<std::pair<P, bool>, std::vector<std::pair<P, bool>>, std::greater<std::pair<P, bool>>>que;
dist2[s][false] = 0;
que.push(std::pair<P, bool>(P(0, s), false));
while (!que.empty())
{
auto p = que.top(); que.pop();
LL v = p.first.second, d = p.first.first, f = p.second;
if (dist2[v][f] < d) continue;
rep(i, G[v].size())
{
edge e = G[v][i];
if (!f && dist2[e.to][true] > dist2[v][false] + 0)
{
dist2[e.to][true] = dist2[v][false] + 0;
que.push(std::pair<P, bool>(P(dist2[e.to][true], e.to), true));
}
if (dist2[e.to][f] > dist2[v][f] + e.cost)
{
dist2[e.to][f] = dist2[v][f] + e.cost;
que.push(std::pair<P, bool>(P(dist2[e.to][f], e.to), f));
}
}
}
}
int main()
{
LL N, M;
in >> N >> M;
std::vector<LL>a(M), b(M), c(M);
rep(i, M) in >> a[i] >> b[i] >> c[i];
dist1.resize(N + 1); dist2.resize(N + 1, std::vector<LL>(2, inf)), G.resize(N + 1);
rep(i, M)
{
G[a[i]].push_back({ b[i],c[i] });
G[b[i]].push_back({ a[i],c[i] });
}
std::vector<LL>ans(N + 1);
dijkstra1(1);
for (LL i = 1; i <= N; ++i) ans[i] += dist1[i];
dijkstra2(1);
for (LL i = 1; i <= N; ++i) ans[i] += std::min(dist2[i][true], dist2[i][false]);
for (LL i = 1; i <= N; ++i) out << ans[i] << std::endl;
}
Bwambocos