洛谷P2888 [USACO07NOV]牛栏Cow Hurdles
题目描述
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.
奶牛们为了比赛要刻苦训练跳木桩。现在有n个木桩,并知道其中m对木桩的高度差。问奶牛们能从木桩u跳到木桩v,最少的跳跃高度是多少?
输入输出格式
输入格式:
-
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
输出格式:
- 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.
输入输出样例
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
4
8
-1
裸floyd
有个坑点是,跳柱子还神tm是单向的……
1 /*by SilverN*/
2 #include<algorithm>
3 #include<iostream>
4 #include<cstring>
5 #include<cstdio>
6 #include<cmath>
7 #include<vector>
8 using namespace std;
9 const int mxn=360;
10 int read(){
11 int x=0,f=1;char ch=getchar();
12 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14 return x*f;
15 }
16 int mp[mxn][mxn];
17 int n,m,T;
18 int main(){
19 n=read();m=read();T=read();
20 int i,j;
21 int u,v,d;
22 memset(mp,0x3f,sizeof mp);
23 for(i=1;i<=m;i++){
24 u=read();v=read();d=read();
25 mp[u][v]=min(mp[u][v],d);
26 }
27 for(int k=1;k<=n;++k)
28 for(i=1;i<=n;++i)
29 for(j=1;j<=n;++j)
30 mp[i][j]=min(mp[i][j],max(mp[i][k],mp[k][j]));
31 while(T--){
32 u=read();v=read();
33 if(mp[u][v]==0x3f3f3f3f)printf("-1\n");
34 else printf("%d\n",mp[u][v]);
35 }
36 return 0;
37 }