結果

問題 No.160 最短経路のうち辞書順最小
ユーザー uryo1903uryo1903
提出日時 2020-01-19 14:33:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,257 bytes
コンパイル時間 2,794 ms
コンパイル使用メモリ 176,928 KB
実行使用メモリ 4,568 KB
最終ジャッジ日時 2023-09-15 20:01:03
合計ジャッジ時間 4,436 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//header{{{
#include<bits/stdc++.h>
  
using namespace std;
 
#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod = 1e9+7; 
constexpr int MOD = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A,B>;
  
int gcd(int a,int b){ return b ? gcd(b,a % b) : a;}
int lcm(int a,int b){ return a / gcd(a,b) * b;}

constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};
//}}}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n,m,S,G;
    cin >> n >> m >> S >> G;

    vector<vector<pii> > g(n);

    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        g[a].emplace_back(b,c);
        g[b].emplace_back(a,c);
    }

    vector<int> min_cost(n,LINF),res(n,99999);
    min_heap<pii> pq;
    res[S] = S;
    min_cost[S] = 0;
    pq.emplace(0,S);

    while(!pq.empty()){
        int now,cost;
        tie(cost,now) = pq.top();
        pq.pop();
        if(min_cost[now] < cost) continue;
        rep(i,g[now].size()){
            int next,ncost;
            tie(next,ncost) = g[now][i];
            if(chmin(min_cost[next],min_cost[now] + ncost)){
                pq.emplace(min_cost[next],next);
                res[next] = now;
            }else if(min_cost[next] == min_cost[now] + ncost && chmin(res[next],now)){
                pq.emplace(min_cost[next],next);
            }
        }
    }

    int now = G;
    vector<int> ans;
    ans.emplace_back(G);
    for(int i = 0;res[now] != now;i++){
        ans.emplace_back(res[now]);
        now = res[now];
    }

    reverse(all(ans));
    rep(i,ans.size()){
        cout << (i == 0 ? "" : " ") << ans[i];
    }
    cout << '\n';

    return 0;
}
0