Cow Hurdles
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6155
Accepted: 2760
Description
Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little energy as possible to jump over the hurdles.
Obviously, it is not very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very stressful. Thus, the cows are only concerned about the height of the tallest hurdle they have to jump over.
The cows' practice room has N (1 ≤ N ≤ 300) stations, conveniently labeled 1..N. A set of M (1 ≤ M ≤ 25,000) one-way paths connects pairs of stations; the paths are also conveniently labeled 1..M. Path i travels from station Si to station Ei and contains exactly one hurdle of height Hi (1 ≤ Hi ≤ 1,000,000). Cows must jump hurdles in any path they traverse.
The cows have T (1 ≤ T ≤ 40,000) tasks to complete. Task i comprises two distinct numbers, Ai and Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N), which connote that a cow has to travel from station Ai to station Bi (by traversing over one or more paths over some route). The cows want to take a path the minimizes the height of the tallest hurdle they jump over when traveling from Ai to Bi . Your job is to write a program that determines the path whose tallest hurdle is smallest and report that height.
Input
* Line 1: Three space-separated integers: N, M, and T
* Lines 2..M+1: Line i+1 contains three space-separated integers: Si , Ei , and Hi
* Lines M+2..M+T+1: Line i+M+1 contains two space-separated integers that describe task i: Ai and Bi
Output
* Lines 1..T: Line i contains the result for task i and tells the smallest possible maximum height necessary to travel between the stations. Output -1 if it is impossible to travel between the two stations.
Sample Input
5 6 3
1 2 12
3 2 8
1 3 5
2 5 3
3 4 4
2 4 8
3 4
1 2
5 1
Sample Output
4
8
-1
Source
USACO 2007 November Silver
题目大意:John想为农场的奶牛举办跳高比赛。奶牛们现在都累了,它们想尽可能的用最少的能量
完成跳高,因为跳过低点的障碍不是很困难,但是高点的障碍就非常困难,所以奶牛只关心它要越
过的障碍的最高高度。
现在给你N个点,编号为1~N,在N个点之间有M个障碍,给你M个障碍链接的点编号和障碍高度,
判断T组从点A跳到点B,尽可能使障碍高度低的路径上最高障碍物高度是多少。
思路:把障碍的高度看做是边,那么题目意思就是给你N个点,M个单向边。问点A到点B能达到的
最长边尽可能短的路径上最长边为多少。类似于求多源最短路径,用Floyd来做,更新最短路径距离
的时候改一下,变成求能达到的路径上最长边最小为多少。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 330;
const int INF = 0xffffff0;
int Map[MAXN][MAXN],Dist[MAXN][MAXN];
void Floyd(int N)
{
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
Dist[i][j] = Map[i][j];
for(int k = 1; k <= N; ++k)
{
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= N; ++j)
{
int tMax; //这里边求的是能达到的路径上最长边最小为多少
if(Dist[i][k] > Dist[k][j])
tMax = Dist[i][k];
else
tMax = Dist[k][j];
if(Dist[i][j] > tMax)
Dist[i][j] = tMax;
}
}
}
}
int main()
{
int N,M,T;
while(~scanf("%d%d%d",&N,&M,&T))
{
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
Map[i][j] = INF;
for(int i = 1; i <= M; ++i)
{
int s,e,h;
scanf("%d%d%d",&s,&e,&h);
Map[s][e] = h;
}
Floyd(N);
for(int i = 1; i <= T; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
if(Dist[a][b] != INF)
printf("%d\n",Dist[a][b]);
else
printf("-1\n");
}
}
return 0;
}