結果

問題 No.2914 正閉路検出
ユーザー GOTKAKO
提出日時 2024-10-04 22:32:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,855 bytes
コンパイル時間 2,694 ms
コンパイル使用メモリ 215,180 KB
最終ジャッジ日時 2025-02-24 15:29:04
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 29 WA * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<vector<tuple<int,int,long long>>> Graph(N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        int w; cin >> w;
        Graph.at(u).push_back({v,i,-w});
        Graph.at(v).push_back({u,i,w});
    }

    vector<bool> already(N);
    vector<long long> dist(N,1e18);
    priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<>> Q;
    dist.at(0) = 0; Q.push({0,0});
    
    int start = -1;
    while(Q.size()){
        auto [nowd,pos] = Q.top(); Q.pop();
        if(nowd != dist.at(pos)) continue;
        if(already.at(pos)){start = pos; break;}
        already.at(pos) = true;
        
        for(auto [to,epos,w] : Graph.at(pos)){
            if(dist.at(to) > dist.at(pos)+w){
                dist.at(to) = dist.at(pos)+w;
                Q.push({dist.at(to),to});
            }
        }
    }
    if(start == -1){cout << "-1\n"; return 0;}

    bool end = false;
    vector<int> route;
    already.assign(M,false);
    auto dfs = [&](auto dfs,int pos,long long nowd) -> void {
        if(route.size() && pos == start){
            if(nowd >= 0) return;
            end = true;
            cout << route.size() << endl;
            cout << start+1 << endl;
            for(int i=0; i<route.size(); i++){
                if(i) cout << " ";
                cout << route.at(i)+1;
            }
            cout << endl;
            return;
        }
        for(auto [to,epos,w] : Graph.at(pos)){
            if(already.at(epos)) continue;
            already.at(epos) = true;
            route.push_back(epos);
            dfs(dfs,to,nowd+w);
            route.pop_back();
            if(end) break;
        }
    };
    dfs(dfs,start,0);
}
0