結果

問題 No.160 最短経路のうち辞書順最小
ユーザー chakkuchakku
提出日時 2020-03-23 18:43:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 1,843 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 179,340 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-27 06:04:15
合計ジャッジ時間 3,712 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
    os << "{";
    for(auto e : v){
        os << e << ",";
    }
    os << "}";
    return os;
}

using ll = long long;
constexpr long long linf = numeric_limits<long long>::max()/3;

struct edge{
    int from,to;
    ll cost;
    edge(int f,int t,ll c) : from(f), to(t), cost(c){}
    edge(){}
};

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

    vector<vector<edge>> G(n);

    vector<vector<ll>> dist(n,vector<ll>(n,linf));
    for(int i=0;i<n;i++) dist[i][i] = 0;

    for(int i=0;i<m;i++){
        int a,b,c; cin >> a >> b >> c;
        G[a].emplace_back(a,b,c);
        G[b].emplace_back(b,a,c);
        dist[a][b] = c;
        dist[b][a] = c;
    }

    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
            }
        }
    }
    //cout << dist[s][g] << endl;

    //for(int i=0;i<n;i++){
    //    for(int j=0;j<n;j++){
    //        if(dist[i][j] == linf) cout << "X" << " ";
    //        else cout << dist[i][j] << " ";
    //    }
    //    cout << endl;
    //}

    vector<int> ans;
    ans.push_back(s);
    int now = s;
    for(int i=0;i<n;i++){
        vector<int> nexts;
        for(auto e : G[now]){
            if(e.cost + dist[e.to][g] == dist[now][g]){
                nexts.emplace_back(e.to);
            }
        }
        sort(nexts.begin(), nexts.end());
        //cout << now << " " << nexts << endl;
        //assert(nexts.size() > 0);
        ans.push_back(nexts[0]);
        now = nexts[0];
        if(now == g){
            break;
        }
    }

    for(int i=0;i<ans.size();i++){
        if(i) cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
0