結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using weight = int64_t;
using edge = pair<int, weight>;
using vertice = vector<edge>;
using graph = vector<vertice>;
using cost = tuple<int64_t, int, int>;

int main() {
    constexpr int64_t inf = 1e18;
    int t, n, m;
    cin >> t >> n >> m;
    graph to(n);
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        to.at(u).emplace_back(v, w);
        if (t == 0) {
            to.at(v).emplace_back(u, w);
        }
    }
    int64_t ans = inf;
    for (int i = 0; i < n; i++) {
        vector<int64_t> d(n, inf);
        priority_queue<cost, vector<cost>, greater<cost>> q;
        q.emplace(0, i, -1);
        while (not q.empty()) {
            auto [dist, v, from] = q.top();
            q.pop();
            if (d.at(v) <= dist) continue;
            if (dist) {
                d.at(v) = dist;
            }
            for (auto next : to.at(v)) {
                auto [next_v, next_w] = next;
                if (next_v == from) continue;
                int64_t next_dist = dist + next_w;
                if (t == 0) {
                    ans = min(ans, d.at(next_v) + next_dist);
                }
                if (d.at(next_v) <= next_dist) continue;
                q.emplace(next_dist, next_v, v);
            }
        }
        ans = min(ans, d.at(i));
    }
    if (ans == inf) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}
0