Loading

P1613 跑路

P1613 跑路

预处理出所有一次能到的节点,然后floyd

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rint register int
ll n,m,mmap[55][55];
bool g[55][55][55];
void f()
{
	for(rint k=1;k<=50;k++)
	for(rint i=1;i<=n;i++)
	for(rint j=1;j<=n;j++)
	for(rint l=1;l<=n;l++)
	{
		if(g[i][j][k-1]&&g[j][l][k-1])
		{
			g[i][l][k]=true;
			mmap[i][l]=1;
		}
	}
}
void floyd()
{
	for(rint k=1;k<=n;k++)
	for(rint i=1;i<=n;i++)
	for(rint j=1;j<=n;j++)
	mmap[i][j]=min(mmap[i][j],mmap[i][k]+mmap[k][j]);
}
int main() {
	scanf("%lld%lld",&n,&m);
	ll u,v;
	memset(mmap,0x3f,sizeof(mmap));
	for(rint i=1;i<=m;i++)
	{
		scanf("%lld%lld",&u,&v);
		g[u][v][0]=true;
		mmap[u][v]=1;
	}
	f();
	floyd();
	printf("%lld\n",mmap[1][n]);
	return 0;
}
posted @ 2020-02-21 15:56  zzctommy  阅读(96)  评论(0编辑  收藏  举报