Atcoder ABC 061D Score Attack 最长路+判环

点击打开链接

题意:n点,m边的有向带权图,问从1出发到n的最大值为?,若可以无穷大 则输出infn,m<=2e3
求最长路-> 把带权的边变号,等价于求出最短路并判断是否有负环即可
注意该图为有向图,n不一定在负环上,所以用bellmanford求出环上的点  或者 暴力:如果第n-1次的d[n] 不等于 第2*n次的d[n]则一定存在环 

#include 
using namespace std;
typedef long long ll;
const ll inf=1ll<<50;
typedef pair ii;
const int N=4e3+20;
ll d[N],n,m;
vector e[N];
bool neg[N];
void calc()
{
	fill(d,d+1+n,inf);
	memset(neg,false,sizeof(neg));
	d[1]=0;
	for(int k=1;kd[i]+w)
					d[v]=d[i]+w;
			}
		}
	}
	
	
	for(int k=1;k<=n;k++)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=0;jd[i]+w)
				{
					d[v]=d[i]+w;
					neg[v]=true;//标记负环上的点 
				}
				if(neg[i]==true)
					neg[v]=true;
			}
		}
	}
	if(neg[n]==false)
		cout<<-d[n]<>n>>m)
	{
		for(int i=0;i<=n;i++)
			e[i].clear();
		ll u,v,w;
		while(m--)
		{
			cin>>u>>v>>w;
			e[u].push_back(ii(v,-w));	
		}
		calc();	
	}
	return 0;
} 


你可能感兴趣的:(Graph,Theory,-,最短路)