結果

問題 No.160 最短経路のうち辞書順最小
ユーザー face4face4
提出日時 2018-10-21 21:03:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,482 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 81,536 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 20:22:02
合計ジャッジ時間 2,202 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 7 ms
5,376 KB
testcase_06 AC 10 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 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 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 4 ms
5,376 KB
testcase_24 AC 4 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 15 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

typedef long long ll;
const int INF = 1ll<<30;

struct Edge{int to; int cost;};

int n, m, s, t, u, v, a, b;

// distは予めINF埋めされているものとする
void dijkstra(int from, vector<vector<Edge>> &path, int *dist){
    dist[from] = 0;
    priority_queue<pair<int, int>> pq;

    pq.push({dist[from], from});

    while(!pq.empty()){
        pair<int, int> now = pq.top();   pq.pop();
        int c = -now.first;
        int pos = now.second;
        if(c > dist[pos])    continue;
        for(Edge e : path[pos]){
            if(dist[e.to] > c + e.cost){
                dist[e.to] = c + e.cost;
                pq.push({-dist[e.to], e.to});
            }
        }
    }
}

int main(){
    int n, m, s, g;
    cin >> n >> m >> s >> g;

    vector<vector<Edge>> v(n);
    int a, b, c;
    for(int i = 0; i < m; i++){
        cin >> a >> b >> c;
        v[a].push_back(Edge({b,c}));
        v[b].push_back(Edge({a,c}));
    }

    int dist[n];
    for(int i = 0; i < n; i++)  dist[i] = INF;

    dijkstra(g, v, dist);

    vector<int> ans;
    int pos = s;
    ans.push_back(pos);

    while(pos != g){
        int next = n;
        for(Edge e : v[pos]){
            if(e.cost + dist[e.to] == dist[pos])  next = min(next, e.to);
        }
        pos = next;
        ans.push_back(pos);
    }

    for(int i = 0; i < ans.size(); i++) cout << ans[i] << " \n"[i == ans.size()-1];

    return 0;
}
0