結果

問題 No.160 最短経路のうち辞書順最小
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-01-04 17:27:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,113 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 152,780 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-22 18:44:48
合計ジャッジ時間 3,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,384 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;

const int MAX_N = 210;
vector<pair<int, int> > G[MAX_N];
vector<int> dijkstra(int start, int goal){
	vector<int> dist(MAX_N, INF);
	vector<int> pre(MAX_N, -1);
    dist[start] = 0;//dist[i] := start->iまでの最短距離
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >  que;
    que.push(make_pair(0, start));
    while(!que.empty()){
        int cost, u, t;//今までにかかった時間 現在の頂点
        cost = que.top().first, u = que.top().second;
        que.pop();
        if(dist[u] < cost) continue;
        for (auto tmp : G[u]){
            int v = tmp.first, time = tmp.second;//隣接する頂点 その頂点まで行く時間
            if(dist[v] > dist[u] + time){//u->v
            	dist[v] = dist[u] + time;
            	pre[v] = u;
            	que.push(make_pair(dist[v], v));
            }else if(dist[v] == dist[u] + time){//辞書順最小
            	if(u < pre[v]) pre[v] = u;
            }
        }
    }
    //経路復元
    vector<int> path;
    for (; goal != start; goal = pre[goal]) path.push_back(goal);
    path.push_back(start);
	reverse(path.begin(), path.end());
	return path; //start-> * -> goal startからgoalまでの最短経路(辞書順最小)
}

int main(void){
	int n, m, s, g;
	cin >> n >> m >> s >> g;
	rep(i, m){
		int a, b, c; cin >> a >> b >> c;
		G[a].pb(mp(b, c));
		G[b].pb(mp(a, c));
	}
	auto ans = dijkstra(s, g);
	rep(i, ans.size()){
		if(i != ans.size() - 1)printf("%d ", ans[i]);
		else printf("%d\n", ans[i]);
	}
	return 0;
}
0