btd
  • 板块灌水区
  • 楼主2333_qwq
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/4 10:21
  • 上次更新2024/5/4 11:56:19
查看原帖
btd
951212
2333_qwq楼主2024/5/4 10:21

P3385 WA on #1, #7, #8, #10 求助

//Bellman-Ford
#include<bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f
#define maxn 12345
using namespace std;
struct edge{
	int u,v,w;
}e[maxn];
int n,m,s,t,dis[maxn],ans;
void BellmanFord(){
	dis[s]=0;
	for(int t=1;t<n;t++){
		for(int i=1;i<=m;i++){
			if(e[i].w>=0){
				if(dis[e[i].u]<inf)dis[e[i].v]=min(dis[e[i].v],dis[e[i].u]+e[i].w);
				if(dis[e[i].v]<inf)dis[e[i].u]=min(dis[e[i].u],dis[e[i].v]+e[i].w);
			}
			else
				if(dis[e[i].u]<inf)dis[e[i].v]=min(dis[e[i].v],dis[e[i].u]+e[i].w);
		}
	}
}
signed main(){
	cin>>t;
	while(t--){
		cin>>n>>m;
		s=1;
		for(int i=1;i<=m;i++)scanf("%lld%lld%lld",&e[i].u,&e[i].v,&e[i].w);
		for(int i=1;i<=n;i++)dis[i]=inf;
		BellmanFord();
		if(dis[1]<0)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}
2024/5/4 10:21
加载中...