結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 37zigen37zigen
提出日時 2020-12-18 05:54:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 601 ms / 2,000 ms
コード長 1,347 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 110,020 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 07:52:33
合計ジャッジ時間 9,826 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 56 ms
4,348 KB
testcase_08 AC 75 ms
4,348 KB
testcase_09 AC 547 ms
4,348 KB
testcase_10 AC 94 ms
4,348 KB
testcase_11 AC 419 ms
4,348 KB
testcase_12 AC 169 ms
4,348 KB
testcase_13 AC 263 ms
4,348 KB
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 109 ms
4,348 KB
testcase_16 AC 49 ms
4,348 KB
testcase_17 AC 35 ms
4,348 KB
testcase_18 AC 28 ms
4,348 KB
testcase_19 AC 254 ms
4,348 KB
testcase_20 AC 10 ms
4,348 KB
testcase_21 AC 412 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 28 ms
4,348 KB
testcase_29 AC 275 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 46 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 397 ms
4,348 KB
testcase_34 AC 177 ms
4,348 KB
testcase_35 AC 20 ms
4,348 KB
testcase_36 AC 15 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 33 ms
4,348 KB
testcase_39 AC 6 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 253 ms
4,348 KB
testcase_44 AC 126 ms
4,348 KB
testcase_45 AC 601 ms
4,348 KB
testcase_46 AC 57 ms
4,348 KB
testcase_47 AC 264 ms
4,348 KB
testcase_48 AC 234 ms
4,348 KB
testcase_49 AC 27 ms
4,348 KB
testcase_50 AC 1 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 45 ms
4,348 KB
testcase_53 AC 28 ms
4,348 KB
testcase_54 AC 253 ms
4,348 KB
testcase_55 AC 246 ms
4,348 KB
testcase_56 AC 255 ms
4,348 KB
testcase_57 AC 422 ms
4,348 KB
testcase_58 AC 423 ms
4,348 KB
testcase_59 AC 425 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;
int T, N, M;
int U[2000];
int 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 ans = 1e18;
    for (int i = 0; i < M; ++i) {
        ans = min(ans, dijkstra(V[i], U[i], i) + W[i]);
    }
    cout << ((ans == (long long)1e18) ? -1 : ans )<< endl;
}
0