結果

問題 No.807 umg tours
ユーザー treeonetreeone
提出日時 2019-03-22 21:35:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 4,000 ms
コード長 1,607 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 212,488 KB
実行使用メモリ 25,628 KB
最終ジャッジ日時 2024-11-23 18:51:33
合計ジャッジ時間 8,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,760 KB
testcase_01 AC 4 ms
5,888 KB
testcase_02 AC 4 ms
5,760 KB
testcase_03 AC 4 ms
5,760 KB
testcase_04 AC 4 ms
5,760 KB
testcase_05 AC 4 ms
5,760 KB
testcase_06 AC 4 ms
5,888 KB
testcase_07 AC 4 ms
5,760 KB
testcase_08 AC 4 ms
5,760 KB
testcase_09 AC 4 ms
5,888 KB
testcase_10 AC 4 ms
5,760 KB
testcase_11 AC 189 ms
17,820 KB
testcase_12 AC 240 ms
15,196 KB
testcase_13 AC 320 ms
18,252 KB
testcase_14 AC 129 ms
11,336 KB
testcase_15 AC 95 ms
9,796 KB
testcase_16 AC 322 ms
19,012 KB
testcase_17 AC 468 ms
23,508 KB
testcase_18 AC 442 ms
23,536 KB
testcase_19 AC 417 ms
20,252 KB
testcase_20 AC 243 ms
12,664 KB
testcase_21 AC 254 ms
12,852 KB
testcase_22 AC 101 ms
8,832 KB
testcase_23 AC 75 ms
8,192 KB
testcase_24 AC 220 ms
18,852 KB
testcase_25 AC 452 ms
25,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

int n, m;
const int MAX_V = 100010;

struct edge{
   int to, cost;
   edge(int to, int cost):to(to), cost(cost){}
};

struct S{
	int cost, to;
	bool flag;
	S(int cost, int to, bool flag):cost(cost), to(to), flag(flag){}
	bool operator>(const S &s) const{
		return cost > s.cost;
	}
};

vector<edge> G[MAX_V];

struct Dijkstra{
	vector<int> d, e;
	Dijkstra(){}
	Dijkstra(int V){
		d.resize(V, INF);
		e.resize(V, INF);
	}
	void calc(int s){
		d[s] = 0;
		e[s] = 0;
		priority_queue<S, vector<S>, greater<S> > q;
		q.push({d[s], s, false});
		while(!q.empty()){
			S p = q.top(); q.pop();
			int from = p.to;
			int cost = p.cost;
			bool f = p.flag;
			if(d[from] < cost) continue;
			rep(i, 0, G[from].size()){
				int next = G[from][i].to;
				int newCost = cost + G[from][i].cost;
				if(f == false){
					if(d[next] > newCost){
						d[next] = newCost;
						q.push({newCost, next, f});
					}
					if(e[next] > cost){
						e[next] = cost;
						q.push({cost, next, true});
					}
				}else{
					if(e[next] > newCost){
						e[next] = newCost;
						q.push({newCost, next, f});
					}
				}
			}
		}
	}
};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> m;
	rep(i, 0, m){
		int u, v, c;
		cin >> u >> v >> c;
		u--; v--;
		G[u].push_back({v, c});
		G[v].push_back({u, c});
	}
	Dijkstra ds(n);
	ds.calc(0);
	rep(i, 0, n){
		int ans = ds.d[i] + ds.e[i];
		cout << ans << endl;
	}
}
0