POJ3615 Cow Hurdles【Floyd】

Cow Hurdles

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 6155
Accepted: 2760

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

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

Source

USACO 2007 November Silver

题目大意:John想为农场的奶牛举办跳高比赛。奶牛们现在都累了,它们想尽可能的用最少的能量

完成跳高,因为跳过低点的障碍不是很困难,但是高点的障碍就非常困难,所以奶牛只关心它要越

过的障碍的最高高度。

现在给你N个点,编号为1~N,在N个点之间有M个障碍,给你M个障碍链接的点编号和障碍高度,

判断T组从点A跳到点B,尽可能使障碍高度低的路径上最高障碍物高度是多少。

思路:把障碍的高度看做是边,那么题目意思就是给你N个点,M个单向边。问点A到点B能达到的

最长边尽可能短的路径上最长边为多少。类似于求多源最短路径,用Floyd来做,更新最短路径距离

的时候改一下,变成求能达到的路径上最长边最小为多少。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 330;
const int INF = 0xffffff0;
int Map[MAXN][MAXN],Dist[MAXN][MAXN];

void Floyd(int N)
{
    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j)
            Dist[i][j] = Map[i][j];

    for(int k = 1; k <= N; ++k)
    {
        for(int i = 1; i <= N; ++i)
        {
            for(int j = 1; j <= N; ++j)
            {
                int tMax;   //这里边求的是能达到的路径上最长边最小为多少
                if(Dist[i][k] > Dist[k][j])
                    tMax = Dist[i][k];
                else
                    tMax = Dist[k][j];
                if(Dist[i][j] > tMax)
                    Dist[i][j] = tMax;
            }
        }
    }
}

int main()
{
    int N,M,T;
    while(~scanf("%d%d%d",&N,&M,&T))
    {
        for(int i = 1; i <= N; ++i)
            for(int j = 1; j <= N; ++j)
                    Map[i][j] = INF;
        for(int i = 1; i <= M; ++i)
        {
            int s,e,h;
            scanf("%d%d%d",&s,&e,&h);
            Map[s][e] = h;
        }
        Floyd(N);
        for(int i = 1; i <= T; ++i)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(Dist[a][b] != INF)
                printf("%d\n",Dist[a][b]);
            else
                printf("-1\n");
        }
    }

    return 0;
}
时间: 2024-12-28 12:47:20

POJ3615 Cow Hurdles【Floyd】的相关文章

HDU1385 Minimum Transport Cost 【Floyd】+【路径记录】

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7496    Accepted Submission(s): 1918 Problem Description These are N cities in Spring country. Between each pair of cities

【floyd】HDU 1874 畅通工程续

之后的题解偏重实用/总结性质,尽量理解算法本身而不是题,时间复杂度什么的也可以放放. 很久之前做过这个题,当时使用dijkstra做的,关于几个最短路算法,分类的话可以分为以下几种. 1.单源最短路:已知起点(终点),计算从源点到其他各个顶点的最短路径长度. 典型算法:Dijkstra,Bellman-Ford(可以算负的,比较慢),spfa(负权能用,加了松弛操作,速度比较炸天) 2.全局最短路:从一点到另一点,典型如Floyd,A*启发式算法. 重新用floyd写一遍: #include <

POJ2253 Frogger 【Floyd】

讲的是,一只雄青蛙要从一个石头到另外一个石头上去找某只雌青蛙,但是这两个石头隔得太远,青蛙跳不过去,所幸,湖面上还有很多其他石头,所以青蛙可以借助别的石头一步一步地跳向那只雌青蛙所在的石头.显然青蛙可能有多种路径,比如其中一条是 2,3,4,2,1 ,它跳了五次,数字代表每次跳的距离也就是路径上相邻两个石头之间的距离,那么这只青蛙的弹跳能力至少是4才能跳过去.在其他的路径中,可能要求青蛙的弹跳是5,是8,是1,是100,等等,这个问题求青蛙需要的最小弹跳能力.其实也就是个最大值中取最小的问题.

POJ3270 cow sorting 【polya】

题目描述: 给你一个数字序列(每个数字唯一),每次你可以交换任意两个数字,代价为这两个数字的和,问最少用多少代价能把这个序列按升序排列好. 题目的具体做法是参考刘汝佳的<算法艺术与信息学奥赛>大概思路是:以后再用别种方法解, 1.找出初始状态和目标状态.明显,目标状态就是排序后的状态. 2.画出置换群,在里面找循环.例如,数字是8 4 5 3 2 7 明显,                                   目标状态是2 3 4 5 7 8,能写为两个循环:(8 2 7)(4

POJ1734 Sightseeing trip 【Floyd】+【最小环】+【路径记录】

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4830   Accepted: 1857   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

HDU1869 六度分离 【Floyd】

六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4213    Accepted Submission(s): 1718 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说,大意是说,任何2个素

POJ2263&amp;ZOJ1952--Heavy Cargo【Floyd】多源最短路变形

链接:http://poj.org/problem?id=2263 题意:有n个点,m条路,每条路双向的,现在卡车从某点到另一点,卡车的承载无上限,但是马路的承载有上限,问卡车应该承载多少才不会压坏马路. poj2253和它类似,链接:http://poj.org/problem?id=2253 解题报告:Here 就是在两点之间找一条路径,使路径中权值最小的那条边的权值最大,edge数组记录当前路径中最小权值边的权值 #include<cstring> #include<string&

HDU2066 一个人的旅行 【Dijkstra】【Floyd】

一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18484    Accepted Submission(s): 6429 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰

POJ 3615 Cow Hurdles (Floyd算法)

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6142   Accepted: 2752 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