Subtask #0全WA
查看原帖
Subtask #0全WA
1058586
hjk20120612楼主2025/7/30 11:19
#include<bits/stdc++.h>
using namespace std;
int T;
int main() {
    cin>>T;
    for(int t=0;t<T;t++){
        int n,m;
        cin>>n>>m;
        vector<tuple<int,int,int>> edges;
        for(int i=0;i<m;i++){
            int u,v,w;
            cin>>u>>v>>w;
            if(w>=0){
                edges.emplace_back(u,v,w);
                edges.emplace_back(v,u,w);
            }
			else{
                edges.emplace_back(u,v,w);
            }
        }
        vector<long long> dist(n+1,LLONG_MAX);
        vector<bool> reachable(n+1,false);
        dist[1]=0;
        reachable[1]=true;
        bool found=false;
        for(int k=0;k<n;k++){
            bool has_update=false;
            for(const auto &edge:edges){
                int u,v,w;
                tie(u,v,w)=edge;
                if(dist[u]!=LLONG_MAX&&dist[u]+w<dist[v]){
                    if(k==n-1&&reachable[u]){
                        found=true;
                        break;
                    }
                    dist[v]=dist[u]+w;
                    has_update=true;
                    if(reachable[u]&&!reachable[v]){
                        reachable[v]=true;
                    }
                }
            }
            if(found){
                break;
            }
            if(!has_update){
                break;
            }
        }
        cout<<(found?"YES":"NO");
    }
    return 0;
}
2025/7/30 11:19
加载中...