Bellman-Ford #11WA 求助
查看原帖
Bellman-Ford #11WA 求助
494183
mashduihca楼主2022/1/26 11:12

第一组数据,答案是No,程序输出了Yes

#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define f(a,b,c) for(int a=b;a<=c;a++)
#define fcin(a,n) for(int i=1;i<=n;++i)cin>>a[i];
#define _fpcin(a,n) for(int i=1;i<=n;++i){cin>>a[i];
#define tpi template<typename T>inline T
#define vpi template<typename T>inline void
#define tp (T p)
#define c2i constexpr const int
using ll=long long;
using namespace std;
int read(void);
c2i N=114514;
int t,dis[N];
struct node
{
	int to,edge;
};
int main()
{
	t=read();
	while(t--)
	{
		memset(dis,0x3f3f3f3f,sizeof(dis));
		vector<node> g[N];
		dis[1]=0;
		int n=read(),m=read(),cnt=0;
		f(i,1,m)
		{
			int u=read(),v=read(),w=read();
			if(w>=0)
			{
				g[u].push_back({v,w});
				g[v].push_back({u,w});
			}
			else
			g[u].push_back({v,w});
		}
		f(i,1,n-1)
		f(j,1,n)
		for(int k=0;k<g[j].size();++k)
		dis[g[j][k].to]=min(dis[g[j][k].to],dis[j]+g[j][k].edge);	
		f(j,1,n)
		for(int k=0;k<g[j].size();++k)
		if(dis[g[j][k].to]>dis[j]+g[j][k].edge)
		{
			puts("YES");
			goto ed;
		}
		puts("NO");
		ed:
			continue;
	}
	return 0;
}
int read(void)
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
2022/1/26 11:12
加载中...