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 itravels 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 iAi 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

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

Sample Output

4
8
-1

题意:有一头牛,要进行跳木桩训练,已知有n个木桩,而且知道m对木桩之间的高度差。但是它很懒,它想尽可能的跳最小的高度就完成从任意一个木桩到任意一个木桩的跳跃,给m对点,问是否存在最小的跳跃高度使得其能够完成跳跃,如果有就输出最小高度;否则输出-1。

解析:无非就是求个每条路的单边最大值然后取最小那个吗,由于是求任意两木桩之间的所有路径上最大高度差值的最小值,所以我们可以用Floyd算法,对其进行处理,处理之后得到的最终结果即为所求了。

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int e[][];
int n,m,t,u,v,w;
scanf("%d%d%d",&n,&m,&t);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i==j)
e[i][j]=;
else
e[i][j]=INF;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(e[u][v]>w)
e[u][v]=w;
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
e[i][j]=min(e[i][j],max(e[i][k],e[k][j]));
while(t--)
{
scanf("%d%d",&u,&v);
if(e[u][v]!=INF)
printf("%d\n",e[u][v]);
else
printf("-1\n"); }
return ;
}

做题后感:一开始是考虑深收与DJ算法,后来考虑到是可以提问多个,就改成了flaoy算法,以后多提问问题可以多考虑可以预处理全局的算法

OJ 21651::Cow Hurdles(佛罗一德的变式)的更多相关文章

  1. 佛洛依德 c++ 最短路径算法

    //20142880 唐炳辉 石家庄铁道大学 #include<iostream> #include<string> using namespace std; #define ...

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

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

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

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

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

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

  5. codevs 2803 爱丽丝·玛格特罗依德

    二次联通门 : codevs 2803 爱丽丝·玛格特罗依德 /* codevs 2803 爱丽丝·玛格特罗伊德 高精 + 找规律 显然, 能拆3就多拆3 不能拆就拆2 注意特判一下 */ #incl ...

  6. POJ 3615 Cow Hurdles(最短路径flyod)

    Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9337   Accepted: 4058 Descr ...

  7. Luogu P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

  8. 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

    题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...

  9. 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

随机推荐

  1. 2014蓝桥杯B组初赛试题《李白打酒》

    题目描述: 话说大诗人李白,一生好饮.幸好他从不开车.     一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒.     逢店加一倍,遇花喝一斗.     这一路上 ...

  2. Solidity部署问题

    Solidity是一个有诸多限制的语言,部署智能合约以及运行都需要gas. 部署的时候如果程序太大,所需要的gas会超过一个区块的上限,这样就没法部署合约.如果用metamask测试的话会弹出以下警告 ...

  3. 如何在Django模型中管理并发性 orm select_for_update

    如何在Django模型中管理并发性 为单用户服务的桌面系统的日子已经过去了 - 网络应用程序现在正在为数百万用户提供服务,许多用户出现了广泛的新问题 - 并发问题. 在本文中,我将介绍在Django模 ...

  4. 通过event事件来控制红绿灯通行车辆

    事件的初始值为False,所以最开始就是红灯,先模拟红绿灯的规律,设定为每两秒变换一次灯,然后再模拟车辆通行,通过事件来将两者的事件结合起来, 当事件为False时,为红灯,车辆处于等待状态,一直wa ...

  5. Appium移动端自动化测试之元素定位(三)

    1.name定位 driver.find_element_by_id(') driver.find_element_by_id(') driver.find_element_by_name('登录') ...

  6. what is diff. b/w app state & session state

    Application state is a data repository available to all classes in an ASP.NET application. Applicati ...

  7. (转)EASYUI+MVC4通用权限管理平台

    原文地址:http://www.cnblogs.com/hn731/archive/2013/07/15/3190947.html 通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在 ...

  8. 激光样式——第九届蓝桥杯C语言B组(国赛)第二题

    原创 标题:激光样式x星球的盛大节日为增加气氛,用30台机光器一字排开,向太空中打出光柱.安装调试的时候才发现,不知什么原因,相邻的两台激光器不能同时打开!国王很想知道,在目前这种bug存在的情况下, ...

  9. C# static 字段初始值设定项无法引用非静态字段、方法或属性

    问题:字段或属性的问题字段初始值设定项无法引用非静态字段.方法 下面代码出错的原因,在类中定义的字段为什么不能用? public string text = test(); //提示 字段或属性的问题 ...

  10. Mybatis 延迟加载策略

    延迟加载: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. 好处: 先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速 ...