結果

問題 No.807 umg tours
ユーザー aa
提出日時 2019-03-22 23:59:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 4,000 ms
コード長 1,409 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 177,796 KB
実行使用メモリ 23,764 KB
最終ジャッジ日時 2023-08-15 12:10:34
合計ジャッジ時間 7,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 217 ms
14,652 KB
testcase_12 AC 224 ms
14,048 KB
testcase_13 AC 311 ms
17,204 KB
testcase_14 AC 130 ms
9,748 KB
testcase_15 AC 116 ms
8,936 KB
testcase_16 AC 331 ms
18,092 KB
testcase_17 AC 390 ms
21,156 KB
testcase_18 AC 404 ms
21,308 KB
testcase_19 AC 402 ms
23,764 KB
testcase_20 AC 270 ms
14,624 KB
testcase_21 AC 278 ms
14,776 KB
testcase_22 AC 116 ms
8,324 KB
testcase_23 AC 88 ms
7,652 KB
testcase_24 AC 262 ms
18,304 KB
testcase_25 AC 382 ms
21,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

using P = pair<long, int>;

struct edge {int to; long c;};

void solve()
{
	int n, m;
	cin >> n >> m;
	vector<vector<long>> dist(2, vector<long>(n, 1e18));
	dist[0][0] = dist[1][0] = 0;
	vector<edge> g[n];
	while(m--)
	{
		int a, b;
		long c;
		cin >> a >> b >> c;
		a--, b--;
		g[a].push_back({b, c});
		g[b].push_back({a, c});
	}
	priority_queue<P, vector<P>, greater<P>> q, q2;
	q.push({0, 0});
	q2.push({0, 0});
	while(!q.empty() || !q2.empty())
	{
		while (!q.empty())
		{
			auto p = q.top();
			q.pop();
			int v = p.second;
			long c = p.first;
			if (dist[0][v] < c) continue;
			for (auto np : g[v])
			{
				int nv = np.to;
				long nc = np.c + dist[0][v];
				if (dist[0][nv] > nc)
				{
					dist[0][nv] = nc;
					q.push({nc, nv});
				}
				if (dist[1][nv] > dist[0][v])
				{
					dist[1][nv] = dist[0][v];
					q2.push({dist[0][v], nv});
				}
			}
		}
		while(!q2.empty())
		{
			auto p = q2.top();
			q2.pop();
			int v = p.second;
			long c = p.first;
			if (dist[1][v] < c) continue;
			for (auto np : g[v])
			{
				int nv = np.to;
				long nc = np.c + dist[1][v];
				if (dist[1][nv] > nc)
				{
					dist[1][nv] = nc;
					q2.push({nc, nv});
				}
			}
		}

	}
	for (int i = 0; i < n; i++)
	{
		long ans = dist[0][i] + dist[1][i];
		cout << ans << endl;
	}
}

int main(void)
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
0