結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 37zigen37zigen
提出日時 2020-12-18 05:57:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 640 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 1,066 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 07:53:47
合計ジャッジ時間 10,505 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 59 ms
4,348 KB
testcase_08 AC 79 ms
4,348 KB
testcase_09 AC 581 ms
4,348 KB
testcase_10 AC 101 ms
4,348 KB
testcase_11 AC 454 ms
4,348 KB
testcase_12 AC 183 ms
4,348 KB
testcase_13 AC 287 ms
4,348 KB
testcase_14 AC 13 ms
4,348 KB
testcase_15 AC 119 ms
4,348 KB
testcase_16 AC 55 ms
4,348 KB
testcase_17 AC 38 ms
4,348 KB
testcase_18 AC 31 ms
4,348 KB
testcase_19 AC 274 ms
4,348 KB
testcase_20 AC 11 ms
4,348 KB
testcase_21 AC 440 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 32 ms
4,348 KB
testcase_29 AC 309 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 51 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 428 ms
4,348 KB
testcase_34 AC 191 ms
4,348 KB
testcase_35 AC 22 ms
4,348 KB
testcase_36 AC 16 ms
4,348 KB
testcase_37 AC 5 ms
4,348 KB
testcase_38 AC 37 ms
4,348 KB
testcase_39 AC 7 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 283 ms
4,348 KB
testcase_44 AC 140 ms
4,348 KB
testcase_45 AC 640 ms
4,348 KB
testcase_46 AC 60 ms
4,348 KB
testcase_47 AC 286 ms
4,348 KB
testcase_48 AC 255 ms
4,348 KB
testcase_49 AC 30 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 48 ms
4,348 KB
testcase_53 AC 29 ms
4,348 KB
testcase_54 AC 272 ms
4,348 KB
testcase_55 AC 272 ms
4,348 KB
testcase_56 AC 272 ms
4,348 KB
testcase_57 AC 458 ms
4,348 KB
testcase_58 AC 458 ms
4,348 KB
testcase_59 AC 456 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
int T, N, M;
int U[2000], V[2000];
long long W[2000];

long long dijkstra(int src, int dst, int id) {
    vector<vector<pair<int, long long>>> G(N);
    for (int i = 0; i < M; ++i) if (i != id) {
        G[U[i]].push_back(make_pair(V[i], W[i]));
        if (T == 0) {
            G[V[i]].push_back(make_pair(U[i], W[i]));
        }
    }
    vector<long long> d(N, (long long)1e18);
    priority_queue<pair<long long, int>> pq;
    pq.push(make_pair(0, src));
    d[src] = 0;
    while (!pq.empty()) {
        auto p = pq.top();
        auto cost = -p.first;
        auto u = p.second;
        pq.pop();
        if (d[u] < cost) continue;
        for (auto e:G[u]) {
            int v = e.first;
            long long nd = d[u] + e.second;
            if (d[v] > nd) {
                d[v] = nd;
                pq.push(make_pair(-nd, v));
            }
        }
    }
    return d[dst];
}

int main() {
    cin >> T >> N >> M;
    for (int i = 0; i < M; ++i) {
        cin >> U[i] >> V[i] >> W[i];
        --U[i];--V[i];
    }
    long long INF = 1e18;
    long long ans = INF;
    for (int i = 0; i < M; ++i) {
        ans = min(ans, dijkstra(V[i], U[i], i) + W[i]);
    }
    cout << (ans == INF ? -1 : ans )<< endl;
}
0