POJ 3615 Cow hurdles (Floyd algorithm)

Source: Internet
Author: User

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 is practicing jumping ov ER hurdles. They is getting tired, though, so they want to being able to use as little energy as possible to jump over the hurdles.

Obviously, it isn't very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very str Essful. Thus, the cows is only concerned about the height of the tallest hurdle they has to jump over.

The cows ' practice 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 is also conveniently labeled 1. M.Path itravels from station Si to station Ei and contains exactly one hurdle of Heig HT Hi (1≤ hi ≤1,000,000). Cows must jump hurdles on any path they traverse.

The cows has T (1≤ T ≤40,000) tasks to complete.  Task I comprises the distinct numbers, ai and Bi (1≤ aiN; 1≤ BiN), which connote that a cow have to travel from station Ai to station Bi (by traversing OV Er 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, 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 II space-separated integers that describe task I: C13>ai and Bi

Output

* Lines 1. T: Line i contains the result for task I and tells the smallest possible maximum height n Ecessary to travel between the stations. Output-1 if it is impossible to travel between the stations.

Sample Input

5 6 31 2 123 2 81 3 52 5 33 4 42 4 83 41 25 1

Sample Output

48-1

Source

Usaco November Silver



Test instructions: There is a bull, to do the stakes training, known to have N Stakes, and know the height difference between the M-stakes. But it is very lazy, it wants to jump as far as possible to the minimum height from any stake to any one of the stakes jump, to M-point, ask whether there is a minimum jump height so that it can complete the jump, if there is the output of the minimum height; otherwise output-1.


Parse: Now to record the path "Length", is actually the maximum jump height, in short, this path through the adjacent two stakes between the maximum value, because as long as can skip this height difference of the largest, the height difference of course can jump past. Because it is the minimum value of the maximum height difference between any two stakes, we can use the Floyd algorithm to process it, and the final result after processing is the minimum value of the maximum height.




AC Code:

#include <cstdio> #include <iostream> #include <algorithm>using namespace std; #define INF        123456789int a[302][302];    Maximum height of the minimum matrix int main () {int n, m, T;    int x, y, W; while (scanf ("%d%d%d", &n, &m, &t)!=eof) {for (int i=1; i<=n; i++)//Initialize FO R (int j=1; j<=n; j + +) A[i][j] = i==j?        0:inf;            for (int i=1; i<=m; i++) {//read in height difference scanf ("%d%d%d", &x, &y, &w);          A[x][y] = min (A[x][y], W);                Update max height difference} for (int k=1, k<=n; k++)//floyd for (int i=1; i<=n; i++)                      for (int j=1; j<=n; J + +) {A[i][j] = min (A[i][j], Max (A[i][k], a[k][j]));            } for (int i=1; i<=t; i++) {scanf ("%d%d", &x, &y);       printf ("%d\n", A[x][y]==inf -1:a[x][y]); Output, if it is an INF, it is unreachable, there is no path between them to meet the requirements}} return 0;






POJ 3615 Cow hurdles (Floyd algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.