結果

問題 No.160 最短経路のうち辞書順最小
ユーザー なおなお
提出日時 2015-03-04 14:48:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 521 ms / 5,000 ms
コード長 2,277 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 163,476 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 06:44:29
合計ジャッジ時間 3,568 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))
#define FOR(i, f, t)        for(int(i)=(f);(i)<(t);(++i))
#define RREP(i, n)          for(int(i)=(n)-1;(i)>=0;--(i))
#define mp                  make_pair
#define pb                  push_back
const int MOD = int(1e9+7);

int N,M,S,G;
int res = 0;

template <class _Tc>
class Dijkstra {
public:
    static const int inf = 1<<29;
    int m_num;
    vector<vector<pair<int,_Tc> > > m;
    void init(int n){
        m_num = n;
        m.clear();
        m.resize(n);
    }
    void add(int u, int v, _Tc e){
        m[u].pb(mp(v,e));
    }
    _Tc solve(int u, int v, vector<pair<_Tc,VI> > *pcost = NULL, vector<int> *proute = NULL){
        int n = m_num;
        vector<pair<_Tc,VI> > c(n, mp(inf,VI()));
        vector<int> r(n, -1);
        priority_queue<pair<_Tc,int> > q;
        c[u].first = 0;
        q.push(mp(0, u));

        while(!q.empty()){
            int u = q.top().second; q.pop();
            FOREACH(it,m[u]){
                int v = it->first;
                _Tc nc = c[u].first + it->second;
                VI ncr = c[u].second;
                ncr.push_back(v);
                if(c[v].first > nc || c[v].first == nc && c[v].second > ncr){
                    c[v] = make_pair(nc,ncr); r[v] = u;
                    q.push(mp(-nc,v));
                }
            }
        }
        if(pcost) *pcost = c;
        if(proute) *proute = r;
        return c[v].first;
    }
};


int main(){
    do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0);

    cin >> N >> M >> S >> G;
    Dijkstra<int> d;
    d.init(N);

    REP(i,M){
        int a,b,c;
        cin >> a >> b >> c;
        d.add(a,b,c);
        d.add(b,a,c);
    }
    VI route;
    d.solve(S, G, NULL, &route);

    VI rt;
    int i = G;
    while(i != S){
        rt.pb(i);
        i = route[i];
    }

    reverse(rt.begin(), rt.end());
    int n = rt.size();
    cout << S;
    REP(i,n) cout << " " << rt[i];
    cout << endl;

    return 0;
}
0