結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const long long INF = 1LL << 60;

long long dist(const vector<vector<pair<int, long long>>>& g, int s, int t){
    const int N = g.size();
    vector<long long> dist(N, INF);
    dist[s] = 0;
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> qu;
    qu.emplace(0, s);
    while(!qu.empty()){
        auto [curCost, pos] = qu.top(); qu.pop();
        for(auto& nd : g[pos]){
            if(pos == s && nd.first == t) continue;
            if(pos == t && nd.first == s) continue;
            if(curCost + nd.second < dist[nd.first]){
                dist[nd.first] = curCost + nd.second;
                qu.emplace(dist[nd.first], nd.first);
            }
        }
    }
    return dist[t];
}

int main(){
    int T;
    while(cin >> T){
        int N, M; cin >> N >> M;
        vector<vector<pair<int, long long>>> g(N);
        for(int i=0;i<M;i++){
            int u, v, w; cin >> u >> v >> w;
            --u; --v;
            g[u].emplace_back(v, w);
            if(T == 0) g[v].emplace_back(u, w);
        }
        long long res = INF;
        for(int i=0;i<N;i++){
            for(auto nd : g[i]){
                res = min(res, nd.second + dist(g, nd.first, i));
            }
        }
        cout << (res < INF ? res : -1) << endl;
    }

}
0