POJ3615 Cow Hurdles "Floyd"

Source: Internet
Author: User

Cow HurdlesTime limit:1000ms Memory Limit:65536ktotal submissions:6155accepted:2760
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 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 on any path they traverse.

The cows has T (1≤t≤40,000) tasks to complete. Task I comprises II distinct numbers, Ai and Bi (1≤ai≤n; 1≤bi≤n), which connote that a cow have to travel from STA tion 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, 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, 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 bet Ween the stations. Output-1 if it is impossible to travel between the 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 November Silver


John wants to hold a high jump for the cows on the farm. The cows are tired now and they want to use the least amount of energy possible .

Complete the high jump, because the obstacle of jumping too low is not very difficult, but the height of the barrier is very difficult, so the cows only care about the more

The maximum height of the barrier.

Now give you n points, numbered 1~n, there are m barriers between n points, give you the point number of the M barrier link and the height of the barrier,

Judge the T-group to jump from point A to points B, as far as possible to make the height of the obstacle high obstacle height of the path is how much.

Idea: The height of the barrier as the edge, then the title is to give you n points, M one-way side. Ask a point A to B can achieve

The longest side of the longest edge on the shortest path. Similar to multi-source shortest path, use Floyd to do, update the shortest path distance

, change it to the minimum of the longest side of the path to be reached.


#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;                This is what the longest side of the path can reach is minimum 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;}


POJ3615 Cow Hurdles "Floyd"

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.