結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー rlangevinrlangevin
提出日時 2024-02-11 02:31:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,743 ms / 2,000 ms
コード長 2,685 bytes
コンパイル時間 2,834 ms
コンパイル使用メモリ 224,220 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-11 02:31:42
合計ジャッジ時間 17,781 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 9 ms
6,676 KB
testcase_08 AC 42 ms
6,676 KB
testcase_09 AC 1,015 ms
6,676 KB
testcase_10 AC 10 ms
6,676 KB
testcase_11 AC 815 ms
6,676 KB
testcase_12 AC 176 ms
6,676 KB
testcase_13 AC 432 ms
6,676 KB
testcase_14 AC 13 ms
6,676 KB
testcase_15 AC 10 ms
6,676 KB
testcase_16 AC 9 ms
6,676 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 23 ms
6,676 KB
testcase_19 AC 140 ms
6,676 KB
testcase_20 AC 8 ms
6,676 KB
testcase_21 AC 550 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 19 ms
6,676 KB
testcase_29 AC 682 ms
6,676 KB
testcase_30 AC 3 ms
6,676 KB
testcase_31 AC 98 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 1,113 ms
6,676 KB
testcase_34 AC 504 ms
6,676 KB
testcase_35 AC 3 ms
6,676 KB
testcase_36 AC 3 ms
6,676 KB
testcase_37 AC 3 ms
6,676 KB
testcase_38 AC 6 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 14 ms
6,676 KB
testcase_44 AC 8 ms
6,676 KB
testcase_45 AC 1,743 ms
6,676 KB
testcase_46 AC 62 ms
6,676 KB
testcase_47 AC 730 ms
6,676 KB
testcase_48 AC 536 ms
6,676 KB
testcase_49 AC 58 ms
6,676 KB
testcase_50 AC 2 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 129 ms
6,676 KB
testcase_53 AC 70 ms
6,676 KB
testcase_54 AC 313 ms
6,676 KB
testcase_55 AC 326 ms
6,676 KB
testcase_56 AC 315 ms
6,676 KB
testcase_57 AC 970 ms
6,676 KB
testcase_58 AC 972 ms
6,676 KB
testcase_59 AC 948 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll inf = 1LL << 60;

struct Edge {
    int to;
    ll w;
    Edge(int to, ll w) : to(to), w(w) {}
};

struct Query {
    int a, b;
    ll w;
    Query(int a, int b, ll w) : a(a), b(b), w(w) {}
};

using Graph = vector<vector<Edge>>;

template <class T> bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else
        return false;
}

template <class T> void print(vector<T> v) {
    rep(i, v.size()) { cout << v[i] << " "; }
    cout << endl;
    return;
}

vector<ll> dijkstra(Graph &G, int s, set<P> &S) {
    int N = G.size();
    vector<ll> dist(N, inf);
    vector<ll> mindist(N, inf);
    vector<bool> seen(N, false);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;
    Q.push({0, s * (N + 1)});
    while (!Q.empty()) {
        ll u = Q.top().second;
        ll d = Q.top().first;
        int v, p;
        p = u % (N + 1);
        v = u / (N + 1);
        Q.pop();
        if (seen[v])
            continue;
        seen[v] = true;
        dist[v] = d;
        S.insert({v, p - 1});
        for (auto e : G[v]) {
            if (seen[e.to])
                continue;
            ll newdist = dist[v] + e.w;
            if (newdist >= mindist[e.to])
                continue;
            mindist[e.to] = newdist;
            Q.push({mindist[e.to], e.to * (N + 1) + v + 1});
        }
    }
    return dist;
}

int main() {
    int T;
    cin >> T;
    int N, M;
    cin >> N >> M;
    vector<Query> E;
    Graph G(N);
    rep(i, M) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--;
        v--;
        E.push_back(Query(u, v, w));
        G[u].push_back(Edge(v, w));
        if (T == 0)
            G[v].push_back(Edge(u, w));
    }
    ll ans = inf;
    if (T == 0) {
        ll temp = inf;
        rep(s, N) {
            set<P> S;
            vector<ll> D = dijkstra(G, s, S);
            for (auto q : E) {
                if (S.count({q.a, q.b}) || S.count({q.b, q.a}))
                    continue;
                chmin(temp, D[q.a] + D[q.b] + q.w);
            }
            chmin(ans, temp);
        }
    } else {
        rep(s, N) {
            set<P> S;
            ll temp = inf;
            vector<ll> D = dijkstra(G, s, S);
            for (auto q : E) {
                if (q.b != s)
                    continue;
                chmin(temp, D[q.a] + q.w);
            }
            chmin(ans, temp);
        }
    }
    if (ans != inf)
        cout << ans << endl;
    else
        cout << -1 << endl;
    return 0;
}
0