WAon#2求调
查看原帖
WAon#2求调
466451
LYY_yyyy楼主2022/11/20 08:21

RT

Code:

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct node{
	int to,next,w;
}q[10100];
int head[10001];
queue<int> s;
int tot;
int dis[1010];
bool vis[1010];
int cnt[1010];
void build(int u,int v,int w)
{
	q[++tot].w=w;
	q[tot].to=v;
	q[tot].next=head[u];
	head[u]=tot;
} 
bool spfa(int st)
{
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	while(!s.empty()) s.pop();
	s.push(st);
	dis[st]=0;
	vis[st]=1;
	cnt[st]++;
	while(!s.empty())
	{
		int now=s.front();
		s.pop();
		vis[now]=0;
		for(int i=head[now];i;i=q[i].next)
		{
			int to=q[i].to;
			if(dis[to]>dis[now]+q[i].w)
			{
				dis[to]=dis[now]+q[i].w;
				if(!vis[to])
				{
					cnt[to]++;
					vis[to]=1;
					s.push(to);
					if(cnt[to]>=n) return 1;
				}
			}
		}
	}
	return 0;
}
int main()
{
	cin>>n>>m;
	int u,v,w;
	int ans=2e9;
	for(int i=1;i<=m;i++) cin>>u>>v>>w,build(u,v,-w);
	bool flag1=spfa(1);
	if(flag1==0) ans=dis[n];
	bool flag2=spfa(n);
	if(flag2==0) ans=min(ans,dis[1]);
	if(flag1==1&&flag2==1) cout<<"Forever love";
	else cout<<ans;
	return 0;
}

2022/11/20 08:21
加载中...