洛谷 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.
Farmer John 想讓她的奶牛準(zhǔn)備郡級(jí)跳躍比賽,貝茜和她的伙伴們正在練習(xí)跨欄。她們很累,所以她們想消耗最少的能量來(lái)跨欄。 顯然,對(duì)于一頭奶牛跳過(guò)幾個(gè)矮欄是很容易的,但是高欄卻很難。于是,奶牛們總是關(guān)心路徑上最高的欄的高度。 奶牛的訓(xùn)練場(chǎng)中有 N (1 ≤ N ≤ 300) 個(gè)站臺(tái),分別標(biāo)記為1..N。所有站臺(tái)之間有M (1 ≤ M ≤ 25,000)條單向路徑,第i條路經(jīng)是從站臺(tái)Si開始,到站臺(tái)Ei,其中最高的欄的高度為Hi (1 ≤ Hi ≤ 1,000,000)。無(wú)論如何跑,奶牛們都要跨欄。 奶牛們有 T (1 ≤ T ≤ 40,000) 個(gè)訓(xùn)練任務(wù)要完成。第 i 個(gè)任務(wù)包含兩個(gè)數(shù)字 Ai 和 Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N),表示奶牛必須從站臺(tái)Ai跑到站臺(tái)Bi,可以路過(guò)別的站臺(tái)。奶牛們想找一條路徑從站臺(tái)Ai到站臺(tái)Bi,使路徑上最高的欄的高度最小。 你的任務(wù)就是寫一個(gè)程序,計(jì)算出路徑上最高的欄的高度的最小值。
輸入輸出格式
輸入格式:
* 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: 兩個(gè)整數(shù) N, M, T
行 2..M+1: 行 i+1 包含三個(gè)整數(shù) Si , Ei , Hi
行 M+2..M+T+1: 行 i+M+1 包含兩個(gè)整數(shù),表示任務(wù)i的起始站臺(tái)和目標(biāo)站臺(tá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 為一個(gè)整數(shù),表示任務(wù)i路徑上最高的欄的高度的最小值。如果無(wú)法到達(dá),輸出 -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
.
.
.
.
.
分析
水題一道
.
.
.
.
.
.
程序:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,t,g[310][310];int main() {scanf("%d%d%d",&n,&m,&t);memset(g,0x3f,sizeof(g));for (int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);g[u][v]=w;}for (int k=1;k<=n;k++)for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)if (i!=j&&j!=k&&k!=i) g[i][j]=min(g[i][j],max(g[i][k],g[k][j]));for (int i=1;i<=t;i++){int a,b;scanf("%d%d",&a,&b);if (g[a][b]<0x3f3f3f3f) printf("%d\n",g[a][b]); else printf("-1\n");}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/YYC-0304/p/10292852.html
總結(jié)
以上是生活随笔為你收集整理的洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 洛谷 P2935 [USACO09JAN
- 下一篇: 交汇的火力