Subtask#0 WA
查看原帖
Subtask#0 WA
1056313
lihaoze2012楼主2024/8/15 15:14
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int n,m,s,tot=0,t;
const int Ma=500000+10;
const int Mb=100000+10;
const int Mc=2147483647;
long long dis[Mb],first[Mb],to[Ma],w[Ma],nex[Ma],f[Mb],num[Ma];
int from[Ma];
void add(int x,int y,int z){ 
	tot++;
	nex[tot]=first[x];
	first[x]=tot;	
	to[tot]=y;
	w[tot]=z;
	from[tot]=x;
}
bool spfa(){
	queue<int> d;
	d.push(s);
	f[s]=1;
	while(!d.empty()){
		int no=d.front();
		d.pop();
		f[no]=0;
		for(int i=first[no];i;i=nex[i]){
			int x=to[i];
			if(dis[x]>dis[no]+w[i]){
				dis[x]=dis[no]+w[i];
				if(dis[x]>dis[no]+w[i]){
					dis[x]=dis[no]+w[i];
					if(!f[x]){
						d.push(x);
						f[x]=1;
						num[s]++;
						if(num[x]>=n){
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
int main(){
	cin>>t;
	while(t--){
		for(int i=0;i<=Ma;i++){
			to[i]=0;w[i]=0;nex[i]=0,num[i]=0;
		}
		for(int i=0;i<=Mb;i++){
			dis[i]=0;first[i]=0;f[i]=0;
		}
		cin>>n>>m;
		for(int i=0;i<m;i++){
			int x,y,z;
			cin>>x>>y>>z;
			add(x,y,z);
			if(z>=0) {
				add(y,x,z);
			}
		}
		/*for(int i=1;i<=tot;i++){
			cout<<from[i]<<"--"<<to[i]<<":"<<w[i]<<endl;
		}*/
		if(spfa()){
			cout<<"YES"<<endl;
		}else{
			cout<<"NO"<<endl;
		}
	}
    return 0;
}
2024/8/15 15:14
加载中...