題目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are ge ...
題目描述
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.
Farmer John 想讓她的奶牛準備郡級跳躍比賽,貝茜和她的伙伴們正在練習跨欄。她們很累,所以她們想消耗最少的能量來跨欄。 顯然,對於一頭奶牛跳過幾個矮欄是很容易的,但是高欄卻很難。於是,奶牛們總是關心路徑上最高的欄的高度。 奶牛的訓練場中有 N (1 ≤ N ≤ 300) 個站臺,分別標記為1..N。所有站臺之間有M (1 ≤ M ≤ 25,000)條單向路徑,第i條路經是從站臺Si開始,到站臺Ei,其中最高的欄的高度為Hi (1 ≤ Hi ≤ 1,000,000)。無論如何跑,奶牛們都要跨欄。 奶牛們有 T (1 ≤ T ≤ 40,000) 個訓練任務要完成。第 i 個任務包含兩個數字 Ai 和 Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N),表示奶牛必須從站臺Ai跑到站臺Bi,可以路過別的站臺。奶牛們想找一條路徑從站臺Ai到站臺Bi,使路徑上最高的欄的高度最小。 你的任務就是寫一個程式,計算出路徑上最高的欄的高度的最小值。
輸入輸出格式
輸入格式:
-
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
行 1: 兩個整數 N, M, T
行 2..M+1: 行 i+1 包含三個整數 Si , Ei , Hi
行 M+2..M+T+1: 行 i+M+1 包含兩個整數,表示任務i的起始站臺和目標站臺: Ai , 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.
行 1..T: 行 i 為一個整數,表示任務i路徑上最高的欄的高度的最小值。如果無法到達,輸出 -1。
輸入輸出樣例
輸入樣例#1: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輸出樣例#1:
4 8 -1
1 //很簡單的最短路,在 Floyd 上面做微小修改。正常的最短路是 sp(s, e) = min{sp(s, k) + sp(k, e)},這裡最短路的計算方式不是邊權求和而是邊權求最大值,所以改成sp(s, e) = min{max{sp(s, k), sp(k,e)}} 即可。 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 ll read() 6 { 7 ll ret=0,ok=1; 8 char ch=getchar(); 9 while(ch<'0'||ch>'9') 10 { 11 if(ch=='-')ok=-1; 12 ch=getchar(); 13 } 14 for(;ch>='0'&&ch<='9';ch=getchar()) 15 ret=ret*10+ch-'0'; 16 return ret*ok; 17 } 18 ll n,m,t; 19 ll s,h,e; 20 ll a,b; 21 ll sp[500][500]; 22 const int INF=0x7fffffff; 23 int main() 24 { 25 n=read(),m=read(),t=read(); 26 memset(sp,0x3f,sizeof(sp)); 27 for(int i=1;i<=m;i++) 28 { 29 cin>>s>>e>>h; 30 sp[s][e]=h; 31 } 32 for(int i=1;i<=n;i++) 33 sp[i][i]=0; 34 for(int k=1;k<=n;k++) 35 for(int i=1;i<=n;i++) 36 for(int j=1;j<=n;j++) 37 { 38 sp[i][j]=min(sp[i][j],max(sp[i][k],sp[k][j])); 39 } 40 for(int i=1;i<=t;i++) 41 { 42 cin>>a>>b; 43 if(sp[a][b]>=INF) 44 cout<<-1<<endl; 45 else 46 cout<<sp[a][b]<<endl; 47 } 48 return 0; 49 }