結果

問題 No.807 umg tours
ユーザー 25__toma
提出日時 2019-03-24 04:59:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 485 ms / 4,000 ms
コード長 1,660 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 182,332 KB
実行使用メモリ 20,204 KB
最終ジャッジ日時 2024-11-23 19:20:40
合計ジャッジ時間 7,674 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<< #str << " " str<<endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007;// 10^9+7

struct Edge { ll to, cost; };
typedef pair<ll, ll> P;

ll N, M;
vector<vector<Edge>> G;
vector<ll> d, d2;

void dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P>> que;
	d = vector<ll>(N, INFL);
	d[s] = 0;
	que.push({ 0,s });

	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;

		for (auto e : G[v])if (d[e.to] > d[v] + e.cost) {
			d[e.to] = d[v] + e.cost;
			que.push({ d[e.to],e.to });
		}
	}
}

void dijkstra2(int s) {
	priority_queue<P, vector<P>, greater<P>> que;
	d2 = vector<ll>(N, INFL);
	d2[s] = 0;
	que.push({ 0,s });

	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;

		for (auto e : G[v]) {
			ll use0 = d[v];
			ll not0 = d2[v] + e.cost;
			ll score = min(use0, not0);
			if (d2[e.to] > score) {
				d2[e.to] = score;
				que.push({ d2[e.to],e.to });
			}
		}
	}
}

int main()
{
	cin >> N >> M;
	G.resize(N);
	REP(i, M) {
		ll a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}

	dijkstra(0);
	dijkstra2(0);

	REP(i, N)cout << d[i] + d2[i] << endl;


	return 0;
}
0