結果

問題 No.160 最短経路のうち辞書順最小
ユーザー cormorancormoran
提出日時 2016-06-21 17:53:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,268 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 83,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 23:43:13
合計ジャッジ時間 1,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
5,376 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 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 1 ms
5,376 KB
testcase_28 WA -
testcase_29 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<tuple>
#include<numeric>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl
// vector
template<class T> ostream& operator << (ostream &os , const vector<T> &v) {
    for(const T &t : v) os << "\t" << t; return os << endl;
}
template<class T> istream& operator >> (istream &is , vector<T> &v) {
    for(T &a : v) is >> a; return is;
}
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) {
    return os << "<" << v.first << " " << v.second << ">";
}

const int INF = 1 << 30;

bool solve() {
    int N, M, S, G; cin >> N >> M >> S >> G;
    vector<vector<pii>> E(N);
    rep(i, M) {
        int a, b, c; cin >> a >> b >> c;
        E[a].push_back(make_pair(b, c));
        E[b].push_back(make_pair(a, c));        
    }
    
    priority_queue<pii> que;
    vector<int> dist(N, INF);

    que.push(make_pair(S, 0));
    dist[S] = 0;

    while(que.size()) {
        int now = que.top().first;
        int cost = que.top().second; que.pop();
        if(dist[now] < cost) continue;
        for(pii nxt : E[now]) {
            int ncost = cost + nxt.second;
            if(dist[nxt.first] > ncost) {
                dist[nxt.first] = ncost;
                que.push(make_pair(nxt.first, ncost));
            }
        }
    }

    vector<int> road;
    int now = G;
    road.push_back(now);
    while(now != S) {
        int prev = INF;
        for(pii nxt : E[now]) {
            if(dist[nxt.first] + nxt.second == dist[now]) {
                prev = min(prev, nxt.first);
            }
        }
        now = prev;
        road.push_back(now);
    }

    reverse(all(road));
    
    rep(i, road.size()) {
        if(i > 0) cout << " ";
        cout << road[i];
    }
    cout << endl;
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}
0