結果

問題 No.807 umg tours
ユーザー treeone
提出日時 2019-03-22 21:35:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 340 ms / 4,000 ms
コード長 1,607 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 204,660 KB
最終ジャッジ日時 2025-01-06 23:45:54
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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