玄关求调
查看原帖
玄关求调
1235700
I_MUST_AK_IOI楼主2024/8/16 16:53
#include<bits/stdc++.h>
#define int long long
#define min(x,y) (x>y?y:x)
#define re register 
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar_unlocked();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar_unlocked();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar_unlocked();}
	return x*f;
}
const int N = 9010 , M = 10010010;
int n,k;
int num;
int dis[M],check[M];
int g[N][N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	n=read();
	k=read();
	for(re int i=1;i<=n;i++) dis[i] = M;
	for(re int i=1;i<=n;i++){
		int a,b;
		a=read();
		b=read();
		g[a][b] = 1;
	}
	dis[1] = 0;
	for(re int i=1;i<=n;i++){
		int minn=M,shu;
		for(re int j=1;j<=n;j++){
			if(!check[j]&&minn>dis[j]) {
				minn = dis[j];
				shu = j;
			}
			check[shu] = 1;
			for(re int j=1;j<=n;j++){
				if(g[shu][j]>0){
					dis[j] = min(dis[j],minn+g[shu][j]);
				}
			}
		}
	}
	if(dis[k]==M) printf("-1");
	else printf("%lld",dis[k]+1);
	return 0;
}

TLE60分玄关求调

2024/8/16 16:53
加载中...