結果

問題 No.807 umg tours
ユーザー leaf_1415leaf_1415
提出日時 2019-03-22 21:43:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 685 ms / 4,000 ms
コード長 1,384 bytes
コンパイル時間 747 ms
コンパイル使用メモリ 69,776 KB
実行使用メモリ 55,524 KB
最終ジャッジ日時 2024-05-02 23:19:07
合計ジャッジ時間 8,290 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,624 KB
testcase_01 AC 4 ms
10,624 KB
testcase_02 AC 6 ms
10,624 KB
testcase_03 AC 5 ms
10,624 KB
testcase_04 AC 4 ms
10,624 KB
testcase_05 AC 4 ms
10,624 KB
testcase_06 AC 5 ms
10,624 KB
testcase_07 AC 5 ms
10,624 KB
testcase_08 AC 4 ms
10,496 KB
testcase_09 AC 4 ms
10,624 KB
testcase_10 AC 5 ms
10,496 KB
testcase_11 AC 380 ms
47,196 KB
testcase_12 AC 360 ms
35,268 KB
testcase_13 AC 491 ms
46,440 KB
testcase_14 AC 199 ms
26,924 KB
testcase_15 AC 156 ms
22,360 KB
testcase_16 AC 519 ms
49,116 KB
testcase_17 AC 685 ms
55,432 KB
testcase_18 AC 662 ms
55,524 KB
testcase_19 AC 648 ms
53,488 KB
testcase_20 AC 363 ms
30,892 KB
testcase_21 AC 383 ms
31,748 KB
testcase_22 AC 144 ms
19,696 KB
testcase_23 AC 113 ms
17,664 KB
testcase_24 AC 338 ms
42,112 KB
testcase_25 AC 650 ms
54,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct edge{
	llint to, cost;
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

int n, m;
vector<edge> G[100005], G2[200005];
llint dist[100005], dist2[200005];

void dijkstra(vector<edge> G[], llint dist[], llint V)
{
	for(int i = 1; i <= V; i++) dist[i] = inf;
	dist[1] = 0;
	
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push( make_pair(0, 1) );
	
	llint v, d;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(dist[G[v][i].to] > d + G[v][i].cost){
				dist[G[v][i].to] = d + G[v][i].cost;
				Q.push( make_pair(dist[G[v][i].to], G[v][i].to) );
			}
		}
	}
}

int main(void)
{
	cin >> n >> m;
	llint a, b, c;
	for(int i = 1; i <= m; i++){
		cin >> a >> b >> c;
		G[a].push_back(edge(b, c));
		G[b].push_back(edge(a, c));
		
		G2[a].push_back(edge(b, c));
		G2[b].push_back(edge(a, c));
		G2[a+n].push_back(edge(b+n, c));
		G2[b+n].push_back(edge(a+n, c));
		G2[a].push_back(edge(b+n, 0));
		G2[b].push_back(edge(a+n, 0));
	}
	
	dijkstra(G, dist, n);
	dijkstra(G2, dist2, 2*n);
	for(int i = 1; i <= n; i++) dist2[i] = min(dist2[i], dist2[i+n]);
	
	for(int i = 1; i <= n; i++){
		cout << dist[i] + dist2[i] << endl;
	}
	return 0;
}
0