結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー KudeKude
提出日時 2020-12-17 14:02:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 364 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 210,484 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 06:08:35
合計ジャッジ時間 7,230 ms
ジャッジサーバーID
(参考情報)
judge13 / 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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 364 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 243 ms
4,348 KB
testcase_12 AC 18 ms
4,348 KB
testcase_13 AC 87 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 16 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 63 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 360 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 1 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 37 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 7 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 91 ms
4,348 KB
testcase_34 AC 42 ms
4,348 KB
testcase_35 AC 8 ms
4,348 KB
testcase_36 AC 4 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 20 ms
4,348 KB
testcase_39 AC 5 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 4 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 286 ms
4,348 KB
testcase_46 AC 6 ms
4,348 KB
testcase_47 AC 38 ms
4,348 KB
testcase_48 AC 47 ms
4,348 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 1 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 19 ms
4,348 KB
testcase_53 AC 8 ms
4,348 KB
testcase_54 AC 103 ms
4,348 KB
testcase_55 AC 104 ms
4,348 KB
testcase_56 AC 102 ms
4,348 KB
testcase_57 AC 283 ms
4,348 KB
testcase_58 AC 291 ms
4,348 KB
testcase_59 AC 283 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<ll,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

ll dist[2000];
constexpr ll inf = 1002003004005006007;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T, n, m;
    cin >> T >> n >> m;
    vector<vector<P>> to(n);
    rep(_, m) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        to[u].emplace_back(v, w);
        if (T == 0) to[v].emplace_back(u, w);
    }
    ll ans = inf;
    rep(s, n) {
        for(auto [t, wt]: to[s]) {
            rep(i, n) dist[i] = inf;
            priority_queue<P, vector<P>, greater<P>> q;
            auto push = [&](int v, ll c) {
                if (c < dist[v]) {
                    dist[v] = c;
                    q.emplace(c, v);
                }
            };
            push(t, 0);
            while(!q.empty()) {
                auto [c, u] = q.top(); q.pop();
                if (c != dist[u]) continue;
                for(auto [v, w]: to[u]) {
                    if (u == t && v == s) continue;
                    push(v, c + w);
                }
            }
            chmin(ans, dist[s] + wt);
        }
    }
    if (ans == inf) ans = -1;
    cout << ans << endl;
}
0