日常只WA一个点,都没地方下手改
查看原帖
日常只WA一个点,都没地方下手改
119638
xia0ji233楼主2022/2/21 16:19
#include<bits/stdc++.h>
#define maxn 20005
using namespace std;
struct eee{
	int next;
	int to;
	int w;
}edge[maxn<<2];
int root[maxn],dis[maxn],e_cnt[maxn],in_que[maxn],cnt;
int n,m;
void add(int x,int y,int w){
	edge[++cnt].to=y;
	edge[cnt].w=w;
	edge[cnt].next=root[x];
	root[x]=cnt;
}
void sync(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int spfa(int s){
	memset(dis,0x3f,sizeof(dis));
	memset(e_cnt,0,sizeof(e_cnt));
	memset(in_que,0,sizeof(in_que));
	queue<int>q;
	dis[s]=0;
	q.push(s);
	e_cnt[s]=0;
	while(q.size()){
		int u=q.front();
		q.pop();
		in_que[u]=0;
		for(int i=root[u];i;i=edge[i].next){
			int v=edge[i].to,w=edge[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				e_cnt[v]=e_cnt[u]+1;
				if(e_cnt[v]>n){
					return false;
				}
				if(!in_que[v]){
					q.push(v);
					in_que[v]=1;
				}	
			}
		}
	}
	return true;
}
int main(){
	freopen("P3385_9.in","r",stdin);
	sync();
	int t;
	cin>>t;
	while(t--){
		cnt=0;
		memset(root,0,sizeof(root));
		memset(edge,0,sizeof(edge));
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int x,y,w;
			cin>>x>>y>>w;
			add(x,y,w);
			if(w>0){
				add(y,x,w);
			}
		}
		if(!spfa(1)){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
			
	}
	return 0;
}
2022/2/21 16:19
加载中...