Cow Hurdles

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9337   Accepted: 4058

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: NM, 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

  1. 5 6 3
  2. 1 2 12
  3. 3 2 8
  4. 1 3 5
  5. 2 5 3
  6. 3 4 4
  7. 2 4 8
  8. 3 4
  9. 1 2
  10. 5 1

Sample Output

  1. 4
  2. 8
  3. -1
  4.  
  5. 题意:
    第一行输入n,m,t三个整数。
    n表示有n个点,m表示有m条路线,t表示有t个奶牛需要经过的路线
    接下来m行描述m条路线,每行表示由xy需要跨过的高度为h
    最后t行表示有t个奶牛,每个奶牛需要从起点s到终点t,问这个奶牛在s -> t过程中需要经过的最低高度是多少
  1. //POJ3615
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. #define INF 123456789
  7. int a[][]; //最大高度的最小值矩阵
  8.  
  9. int main()
  10. {
  11. int n, m, t;
  12. int x, y, h;
  13. while (scanf("%d%d%d", &n, &m, &t) != EOF)
  14. {
  15. for (int i = ; i <= n; i++) //初始化
  16. for (int j = ; j <= n; j++)
  17. {
  18. if(i==j)
  19. a[i][j]=;
  20. else
  21. a[i][j]=INF;
  22. }
  23. for (int i = ; i <= m; i++)//读入高度差
  24. {
  25. scanf("%d%d%d", &x, &y, &h);
  26. a[x][y] = min(a[x][y], h); //更新子问题高度差
  27. }
  28.  
  29. for (int k = ; k <= n; k++) //Floyd
  30. for (int i = ; i <= n; i++)
  31. for (int j = ; j <= n; j++)
  32. {
  33. a[i][j] = min(a[i][j], max(a[i][k], a[k][j]));//DP思想
  34. }
  35. for (int i = ; i <= t; i++)
  36. {
  37. scanf("%d%d", &x, &y);
  38. if (a[x][y] == INF) //输出,如果还是INF,那就代表不可达,两者时之间没有路径满足要求
  39. printf("-1\n");
  40. else
  41. printf("%d\n",a[x][y]);
  42. }
  43. }
  44. return ;
  45. }

POJ 3615 Cow Hurdles(最短路径flyod)的更多相关文章

  1. POJ 3615 Cow Hurdles

    http://poj.org/problem?id=3615 floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边 那么输入的时候可以直接把dist[i][j] 当作i j ...

  2. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  3. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )

    直接floyd.. ---------------------------------------------------------------------------- #include<c ...

  4. 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 424  Solved: 272 ...

  5. poj 3348 Cow 凸包面积

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description ...

  6. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  7. POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)

    POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...

  8. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  9. bzoj1641 / P2888 [USACO07NOV]牛栏Cow Hurdles

    P2888 [USACO07NOV]牛栏Cow Hurdles Floyd $n<=300$?果断Floyd 给出核心式,自行体会 $d[i][j]=min(d[i][j],max(d[i][k ...

随机推荐

  1. mybatis注解中写SQL语句

    参考: https://blog.csdn.net/gebitan505/article/details/54929287/https://blog.csdn.net/KingBoyWorld/art ...

  2. 五、生产者消费者模型_ThreadLocal

    1.生产者消费者模型作用和示例如下:1)通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率 ,这是生产者消费者模型最重要的作用2)解耦,这是生产者消费者模型附带的作用,解耦意味着生产者 ...

  3. RabbitMq学习笔记——RabbitMQ C的使用

    1.需要用到的参数: 主机名:hostname.端口号:port.交换器:exchange.路由key:routingkey .绑定路由:bindingkey.用户名:user.密码:psw,默认用户 ...

  4. Percona-Toolkit 之 pt-kill 用法

    生产环境中我们时常遇到这样的情况,数据库性能恶劣,需要马上杀掉部分会话,不然数据库就夯死.我们可以先找show processlist的输出来杀会话,但是比较麻烦.pt-kill为我们解决了杀会话问题 ...

  5. liunx开源打印驱动foo2zjs编译小坑

    在编译foo2zjs时出现 ## Dependencies...# *** *** Error: gs is not installed! *** *** Install ghostscript (g ...

  6. ActivePerl 安装

    下载 https://www.activestate.com/products/activeperl/downloads/ 链接:https://pan.baidu.com/s/1IXPdYFd5bD ...

  7. Docker registry自签名证书

    权威Registry 获取安全证书有两个办法:互联网认证的CA处获取.自建CA自己给自己签名. 1.从认证CA处获取签名证书,大多数是需要付出一定费用的,近些年也有认证CA提供免费证书,例如Let’s ...

  8. LR、SVM、RF、GBDT、XGBoost和LightGbm比较

    正则化 L1范数 蓝色的是范数的解空间,红色的是损失函数的解空间.L2范数和损失函数的交点处一般在坐标轴上,会使\(\beta=0\),当然并不一定保证交于坐标轴,但是通过实验发现大部分可以得到稀疏解 ...

  9. volume 方式使用 Secret【转】

    Pod 可以通过 Volume 或者环境变量的方式使用 Secret,今天先学习 Volume 方式. Pod 的配置文件如下所示: ① 定义 volume foo,来源为 secret mysecr ...

  10. IDEA自用插件,驼峰插件,MyBatis插件,Lombok插件

    IDEA自用插件 驼峰插件:CamelCase,Shift + Alt + u快速切换驼峰 MyBatisX插件:快速在mapper之间跳转 Lombok插件:注解实现get.set方法 MyBati ...