44分求助
查看原帖
44分求助
564321
ChaseDream楼主2022/6/6 13:14
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
const ll inf=1e18;
ll n,d[2010],m,s,u,v,w,cnt[2010],in[2010];
vector<pair<ll,ll> > g[2010];
queue<int> q;
string kk(){
	cin>>n>>m;
	s=1;
	for(int i=1;i<=n;i++) d[i]=inf,cnt[i]=0,g[i].clear(),in[i]=0;
	for(int i=1;i<=m;i++){
		scanf("%lld%lld%lld",&u,&v,&w);	
		if(w<0) g[u].push_back(make_pair(v,w));
		else{
			g[u].push_back(make_pair(w,u));
			g[v].push_back(make_pair(u,w));
		}
	} 
	
	d[s]=0;
	q.push(s);
	in[s]=1;
	while(!q.empty()){
		u=q.front();
		q.pop();
		in[u]=0;
		for(auto i:g[u]){
			v=i.first;
			w=i.second; 
			if(d[u]+w<d[v]){
				d[v]=d[u]+w;
				if(!in[v]){
					q.push(v);
					cnt[v]++;
					in[v]=1;
					if(cnt[v]>n){
						while(!q.empty()) q.pop();
						return "YES";
					} 
				}
			}
		}

	}
	return "NO";


	return 0;
}
int main(){
	int t;
	cin>>t;
	for(int i=1;i<=t;i++) cout<<kk()<<endl;
	return 0;
}
2022/6/6 13:14
加载中...