結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tsutajtsutaj
提出日時 2017-07-04 20:07:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,680 bytes
コンパイル時間 1,947 ms
コンパイル使用メモリ 171,688 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 16:11:45
合計ジャッジ時間 2,754 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 8 ms
5,376 KB
testcase_06 AC 11 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 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 3 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 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 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 4 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 基本テンプレート (縮小版)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, M, S, G;
int dist[210];
int E[210][210];

signed main() {
    memset(E, -1, sizeof(E));
    cin >> N >> M >> S >> G;
    rep(i,0,M) {
        int a, b, c; cin >> a >> b >> c;
        E[a][b] = c;
        E[b][a] = c;
    }
    fill(dist, dist+N, INF);
    dist[G] = 0;

    priority_queue< pii, vector<pii>, greater<pii> > q;
    q.push( pii(0, G) );
    while(!q.empty()) {
        pii cur = q.top(); q.pop();
        int d = cur.first, pt = cur.second;
        repr(i,N-1,0) {
            if(i == pt || E[i][pt] < 0) continue;
            if(dist[i] > dist[pt] + E[i][pt]) {
                dist[i] = dist[pt] + E[i][pt];
                q.push( pii(dist[i], i) );
            }
        }
    }

    vector<int> ans;
    int pt = S;
    ans.push_back(pt);
    while(pt != G) {
        rep(i,0,N) {
            if(E[pt][i] < 0 || i == pt) continue;
            if(dist[pt] - dist[i] == E[pt][i]) {
                pt = i;
                ans.push_back(pt);
                break;
            }
        }
    }
    rep(i,0,ans.size()) cout << (i==0 ? "" : " ") << ans[i];
    cout << endl;
    return 0;
}
0