BZOJ 1641 USACO 2007 Nov. Cow Hurdles 奶牛跨栏

【题解】

  弗洛伊德。更新距离的时候把$f[i][j]=min(f[i][j],f[i][k]+f[k][j])$改为$f[i][j]=min(f[i][j],max(f[i][k],f[k][j]))$.

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N (400)
#define rg register
using namespace std;
int n,m,t,a[N][N];
inline int read(){
	int k=0,f=1; char c=getchar();
	while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
	while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
	return k*f;
}
int main(){
	n=read(); m=read(); t=read();
	for(rg int i=1;i<=n;i++)
		for(rg int j=1;j<=n;j++) if(i!=j) a[i][j]=1e9;
	for(rg int i=1,u,v;i<=m;i++) u=read(),v=read(),a[u][v]=read();
	for(rg int k=1;k<=n;k++)
		for(rg int i=1;i<=n;i++)
			for(rg int j=1;j<=n;j++) 
			a[i][j]=min(a[i][j],max(a[i][k],a[k][j]));
	for(rg int i=1;i<=t;i++){
		int u=read(),v=read();
		if(a[u][v]==1e9) puts("-1");
		else printf("%d\n",a[u][v]);
	}
	return 0;
}

  

posted @ 2018-02-02 08:14  Driver_Lao  阅读(99)  评论(0编辑  收藏  举报