結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー Kude
提出日時 2020-12-17 14:02:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 2,085 ms
コンパイル使用メモリ 200,324 KB
最終ジャッジ日時 2025-01-17 02:26:14
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

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