結果

問題 No.160 最短経路のうち辞書順最小
ユーザー koprickykopricky
提出日時 2017-07-29 07:41:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,870 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 171,252 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 04:48:14
合計ジャッジ時間 2,543 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 201;

struct edge{
	int to;
	int cost;
};

int d[MAX_N];
vector<edge> G[MAX_N];

void dijkstra(int s)
{
	priority_queue<P,vector<P>,greater<P> > que;
	d[s] = 0;
	que.push(P(0,s));
	while(!que.empty()){
		P p = que.top();
		que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		vector<edge>::iterator it = G[v].begin();
		rep(i,G[v].size()){
			if(d[G[v][i].to] > d[v] + G[v][i].cost){
				d[G[v][i].to] = d[v] + G[v][i].cost;
				que.push(P(d[G[v][i].to],G[v][i].to));
			}
		}
	}
}

int main()
{
    int n,m,s,g;
    cin >> n >> m >> s >> g;
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        G[a].pb({b,c});
        G[b].pb({a,c});
    }
    rep(i,n){
        d[i] = INF;
    }
    dijkstra(g);
    int opt = d[s];
    vector<int> route;
    int nw = s;
    route.pb(s);
    int dir = 0;
    while(nw != g){
        vector<P> vec;
        rep(i,G[nw].size()){
            if(dir+G[nw][i].cost == opt - d[G[nw][i].to]){
                vec.pb({G[nw][i].to,G[nw][i].cost});
            }
        }
        sort(all(vec));
        nw = vec[0].fi;
        dir += vec[0].se;
        route.pb(nw);
    }
    int num = (int)route.size();
    rep(i,num-1){
        cout << route[i] << " ";
    }
    cout << route[num-1] << endl;
    return 0;
}
0