結果

問題 No.160 最短経路のうち辞書順最小
ユーザー snrnsidy
提出日時 2021-06-03 04:47:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,675 bytes
コンパイル時間 3,266 ms
コンパイル使用メモリ 202,580 KB
最終ジャッジ日時 2025-01-21 21:13:34
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

using namespace std;

int dist[201];
int backtracking[201];
vector <pair<int, int>> adj[201];

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N, M, S, G;
    int a, b, c;
    
    cin >> N >> M >> S >> G;
    for (int i = 0; i < M; i++)
    {
        cin >> a >> b >> c;
        adj[a].push_back(make_pair(b, c));
        adj[b].push_back(make_pair(a, c));
    }

    for (int i = 0; i < N; i++)
    {
        dist[i] = 1e9;
    }

    memset(backtracking, -1, sizeof(backtracking));
    dist[S] = 0;
    priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pque;
    pque.push(make_pair(dist[S], S));

    while (!pque.empty())
    {
        int now = pque.top().second;
        pque.pop();

        if (now == G)
        {
            break;
        }

        for (auto it : adj[now])
        {
            int next = it.first;
            int cost = it.second;
            if (dist[next] > dist[now] + cost)
            {
                dist[next] = dist[now] + cost;
                backtracking[next] = now;
                pque.push(make_pair(dist[next], next));
            }
            else if (dist[next] == dist[now] + cost)
            {
                backtracking[next] = min(backtracking[next], now);
            }
        }
    }
       
    stack <int> path;
    int now = G;
    path.push(now);
    while(1)
    {
        now = backtracking[now];
        if (now == -1)
        {
            break;
        }
        path.push(now);
    }

    while (!path.empty())
    {
        cout << path.top() << ' ';
        path.pop();
    }
    cout << '\n';

    return 0;
}
0