博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【洛谷 2888】牛栏
阅读量:4670 次
发布时间:2019-06-09

本文共 3257 字,大约阅读时间需要 10 分钟。

题目描述

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 31 2 123 2 81 3 52 5 33 4 42 4 83 41 25 1
输出样例#1: 
48-1

说明

感谢 @gaozhiyong 提供翻译

题解:水的最短路,注意f[i][j]=min(max(f[i][k],f[k][j]),f[i][j]);即可。

#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;const int oo=21474836;int T,s,t,n,m,f[309][309],x,y,h;int main(){ freopen("2888.in","r",stdin); freopen("2888.out","w",stdout); scanf("%d %d %d",&n,&m,&T); for(int i=1;i<=307;i++){ for(int j=1;j<=307;j++){ f[i][j]=oo; } } for(int i=1;i<=m;i++){ scanf("%d %d %d",&x,&y,&h); f[x][y]=h; } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) f[i][j]=min(max(f[i][k],f[k][j]),f[i][j]); while(T--){ scanf("%d %d",&x,&y); if(f[x][y]!=oo) cout<
<

 

转载于:https://www.cnblogs.com/wuhu-JJJ/p/11182178.html

你可能感兴趣的文章
Shell脚本学习-数组
查看>>
2015年传智播客JavaEE 第168期就业班视频教程day38-SSH综合案例-1
查看>>
day18-事务与连接池 1.复习
查看>>
[原]从一个链接错误探究GCC的链接库顺序
查看>>
PHP面向对象:instanceof 运算符 (备忘)
查看>>
数据存储-CoreData总结
查看>>
通过Ajax的方式执行GP服务
查看>>
Ztree加载完成后显示勾选节点
查看>>
HDU 3401
查看>>
asp.net中XmlDocument解析出现出错,处理特殊字符
查看>>
unable to locate package gparted
查看>>
Centos7安装Mysql
查看>>
Hadoop伪分布安装配置
查看>>
idhttp.post方式 调用datasnap rest 远程方法(转咏南兄)
查看>>
jQuery包裹节点用法完整示例
查看>>
EL表达式中,param和requestScope的区别
查看>>
AngularJs angular.element
查看>>
利用HUtool读取Excel内容
查看>>
浅析libuv源码-node事件轮询解析(1)
查看>>
JS——try catch throw
查看>>