結果

問題 No.160 最短経路のうち辞書順最小
ユーザー snrnsidysnrnsidy
提出日時 2021-06-03 04:55:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,724 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 208,948 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 19:56:50
合計ジャッジ時間 3,279 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 WA -
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 9 ms
6,940 KB
testcase_06 AC 10 ms
6,944 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
6,944 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 3 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
6,944 KB
testcase_28 WA -
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

using namespace std;

int dp[201][201];
int history[201][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++)
    {
        for (int j = 0; j < N; j++)
        {
            dp[i][j] = 1e9;
        }
    }

    dp[0][S] = 0;
    memset(history, -1, sizeof(history));

    for (int i = 1; i <= N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            for (auto it : adj[j])
            {
                int cost = it.second;
                int k = it.first;
                if (dp[i][j] > dp[i - 1][k] + cost)
                {
                    dp[i][j] = dp[i - 1][k] + cost;
                    history[i][j] = k;
                }
                else if (dp[i][j] == dp[i - 1][k] + cost)
                {
                    history[i][j] = min(history[i][j], k);
                }
            }
        }
    }

    long long int MIN = 1e9;
    int len = 0;
    int pos = 0;

    for (int i = 0; i <= N; i++)
    {
        if (MIN > dp[i][G])
        {
            MIN = dp[i][G];
            len = i;
            pos = G;
        }
    }

    stack <int> path;

    while (1)
    {
        path.push(pos);
        if (pos == S)
        {
            break;
        }
        pos = history[len][pos];
        len--;
    }

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

    return 0;
}
0