蒟蒻求助&&急&&悬关
查看原帖
蒟蒻求助&&急&&悬关
719619
drinktowind楼主2023/11/4 14:45
#include<bits/stdc++.h>
using namespace std;
struct node{
	int next,to,w;
}a[10010];
int head[10010],dis[1010],sum[1010],cnt,n,mn=2e9;
bool f[1010];
queue<int> q;
void add(int u,int v,int w)
{
	a[++cnt].to=v;
	a[cnt].w=-w;
	a[cnt].next=head[u];
	head[u]=cnt;
}
void init()
{
	for(int i=1;i<=n;i++)
		dis[i]=1e9;
	memset(f,0,sizeof(f));
	memset(sum,0,sizeof(sum));
}
bool SPFA(int x)
{
	q.push(x);
	dis[x]=0;
	while(!q.empty())
	{
		x=q.front();
		q.pop(); 
		f[x]=1;;
		if(sum[x]>n)
		{
			cout<<"Forever love";
			return 0;
		}
		for(int i=head[x];i;i=a[x].next)
		{
			if(dis[a[i].to]>dis[x]+a[i].w)
			{
				dis[a[i].to]=dis[x]+a[i].w;
				sum[a[i].to]=sum[x]+1;
				if(sum[a[i].to]>n)
				{
					cout<<"Forever love";
					return 0;
				}
				if(!f[a[i].to])
				{
					f[a[i].to]=1;
					q.push(a[i].to);
				}
			}
		}
	}
	return 1;
}
int main()
{
	int m,u,v,w;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>u>>v>>w;
		add(u,v,w);
	}
	init();
	if(!SPFA(1))
		return 0;
	else
		mn=dis[n];
	init();
	if(!SPFA(n))
		return 0;
	else
		mn=min(mn,dis[1]);
	cout<<mn<<endl;
	return 0;
}

在wangsiyuanZP大佬的hack下,无输出,大概率死循环,不知道怎么改,求

4 4
1 4 0
1 2 1
2 3 1
3 2 1

正确输出:0

2023/11/4 14:45
加载中...