結果

問題 No.160 最短経路のうち辞書順最小
ユーザー snrnsidysnrnsidy
提出日時 2021-06-03 04:50:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,837 bytes
コンパイル時間 2,191 ms
コンパイル使用メモリ 210,576 KB
実行使用メモリ 814,620 KB
最終ジャッジ日時 2024-04-27 19:51:19
合計ジャッジ時間 5,858 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
        if (dist[now] < pque.top().first)
        {
            pque.pop();
            continue;
        }
        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);
                pque.push(make_pair(dist[next], next));
            }
        }
    }
       
    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