結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー rlangevinrlangevin
提出日時 2024-02-11 02:24:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,691 bytes
コンパイル時間 2,946 ms
コンパイル使用メモリ 225,948 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-11 02:24:45
合計ジャッジ時間 28,616 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 3 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 117 ms
6,676 KB
testcase_08 AC 199 ms
6,676 KB
testcase_09 AC 1,735 ms
6,676 KB
testcase_10 AC 157 ms
6,676 KB
testcase_11 AC 1,360 ms
6,676 KB
testcase_12 AC 400 ms
6,676 KB
testcase_13 AC 788 ms
6,676 KB
testcase_14 AC 84 ms
6,676 KB
testcase_15 AC 169 ms
6,676 KB
testcase_16 AC 7 ms
6,676 KB
testcase_17 AC 65 ms
6,676 KB
testcase_18 AC 65 ms
6,676 KB
testcase_19 AC 330 ms
6,676 KB
testcase_20 AC 36 ms
6,676 KB
testcase_21 AC 820 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 1 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 114 ms
6,676 KB
testcase_29 AC 1,471 ms
6,676 KB
testcase_30 AC 3 ms
6,676 KB
testcase_31 AC 224 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 1,750 ms
6,676 KB
testcase_34 AC 832 ms
6,676 KB
testcase_35 AC 3 ms
6,676 KB
testcase_36 AC 3 ms
6,676 KB
testcase_37 AC 2 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 222 ms
6,676 KB
testcase_44 AC 110 ms
6,676 KB
testcase_45 TLE -
testcase_46 AC 152 ms
6,676 KB
testcase_47 AC 1,117 ms
6,676 KB
testcase_48 AC 1,087 ms
6,676 KB
testcase_49 AC 122 ms
6,676 KB
testcase_50 AC 2 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 211 ms
6,676 KB
testcase_53 AC 113 ms
6,676 KB
testcase_54 AC 615 ms
6,676 KB
testcase_55 AC 618 ms
6,676 KB
testcase_56 AC 618 ms
6,676 KB
testcase_57 AC 1,534 ms
6,676 KB
testcase_58 AC 1,536 ms
6,676 KB
testcase_59 AC 1,531 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});
        S.insert({p - 1, v});
        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}))
                    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