結果

問題 No.807 umg tours
ユーザー BwambocosBwambocos
提出日時 2019-03-22 22:16:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 573 ms / 4,000 ms
コード長 2,054 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 190,644 KB
実行使用メモリ 37,120 KB
最終ジャッジ日時 2023-08-15 12:00:55
合計ジャッジ時間 9,294 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 281 ms
23,220 KB
testcase_12 AC 323 ms
21,792 KB
testcase_13 AC 408 ms
26,948 KB
testcase_14 AC 176 ms
13,924 KB
testcase_15 AC 136 ms
11,516 KB
testcase_16 AC 430 ms
29,240 KB
testcase_17 AC 573 ms
37,120 KB
testcase_18 AC 565 ms
36,292 KB
testcase_19 AC 542 ms
32,352 KB
testcase_20 AC 319 ms
20,100 KB
testcase_21 AC 318 ms
20,556 KB
testcase_22 AC 137 ms
10,832 KB
testcase_23 AC 99 ms
8,964 KB
testcase_24 AC 312 ms
32,488 KB
testcase_25 AC 569 ms
37,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0