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;
}

  

原文地址:https://www.cnblogs.com/DriverLao/p/8403359.html

时间: 2024-10-05 10:16:44

BZOJ 1641 USACO 2007 Nov. Cow Hurdles 奶牛跨栏的相关文章

BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )

直接floyd.. ---------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ;  i < n ; ++i ) #define clr( x

1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 424  Solved: 272[Submit][Status] Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高栏却很难.于是,奶牛们总是关心路径上最高的栏的高度. 奶牛的训练场

BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高栏却很难.于是,奶牛们总是关心路径上最高的栏的高度. 奶牛的训练场中有 N (1 ≤ N ≤ 300) 个站台,分别标记为1..N.所有站台之间有M (1 ≤ M ≤ 25,000)条单向路径,第i条路经是从站台Si开始,到站台Ei,其中最高的栏的高度为Hi (1 ≤ Hi ≤ 1,000,000)

BZOJ 1706 usaco 2007 Nov relays 奶牛接力跑/POJ 3613 Cow Relays 倍增Floyd

题目大意:求恰好走k步从S到T的最短路. 思路:设f[p][i][j]为从i到j恰好走2^p步的最短路,DP方程十分简单:f[p][i][j] = min(f[p][i][j],f[p - 1][i][k] + f[p - 1][k][j]); 对总步数T进行二进制拆分,在T有1的位置上,假如这个位置为p,那么就用f[p][][]来更新答案g[][],最后得到的g[][]就是答案矩阵. 注意要离散化一下.. CODE: #include <cstdio> #include <cstrin

【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到E,恰好经过N条边的最短路径(姑且称为路径吧,虽然好像已经不是了……) 总共只有大约200个点(很多点根本没走到,离散化一下即可)所以可以考虑Floyd算最短路. 引用下题解: 题目求i,j之间边数恰为N的最短路径(边可以重复走),我们知道线性代数中有:01邻接矩阵A的K次方C=A^K,C[i][j

奶牛接力 (Cow Relays, USACO 2007 Nov)

题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture. Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each

BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19226   Accepted: 8775 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of 

【POJ3613】【USACO 2007 Nov Gold】 2.Cow Relays 矩阵乘法?

题意:给你一个m条边的图,求s到t的正好用k条边的最短路. (输入k,t,s,t) 题解: 先说说暴力. 动规f[k][i][j]表示i到j经过k条边的最短路,然后外层循环k一遍遍跑最后出解. 显然大概率T. 然后有一种思路: 我们可以动规求得f[k][i][j]表示i到j经过k条边的最短路,然后再求g[i]表示从终点走i步回到终点的最短路. 这样我们就可以乱搞过了.(没写,也没细想) 再之后是正解: 我们可以利用类似于快速幂的方法求f[i][j]表示i到j正好用多少多少步. 然后思想是使用边数

bzoj:1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. Only cows can perform the