HDU 1598 find the most comfortable road (minimum spanning tree)

Source: Internet
Author: User

Find the most comfortable road

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5897 Accepted Submission (s): 2562


Problem Description
XX star There are many cities, between the city through a strange highway SARS (Super Air Roam Structure---Super air roaming structure) to communicate, each SARS on the Flycar in the above limit the fixed speed, while XX star people Flycar "Comfort" has special requirements, that is, the highest speed during the ride and the lowest speed difference between the more comfortable ride, (understood as the speed limit requirements of SARS, Flycar must be instantaneous speed up/down, pain),
But XX star people on time but not so many requirements. I want you to find the most comfortable route between the cities. (Sars is two-way).


Input
The input includes multiple test instances, each of which includes:
The first line has 2 positive integers n (1<n<=200) and M (m<=1000), which indicates that there are N cities and M-strips of SARS.
The next line is three positive integer startcity,endcity,speed, representing the startcity to endcity from the surface, and the speed limit to Speedsars. speed<=1000000
Then there is a positive integer Q (q<11), which indicates the number of paths to be searched.
The next Q line has 2 positive integer start,end, which indicates the end of the pathfinding.


Output
Each pathfinding requires a line to be printed, and only a non-negative integer is output to indicate the maximum comfort and minimum speed difference between the best route. If the start and end point cannot be reached, then output-1.


Sample Input
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2


Sample Output
1

0


The maintrouble: xx star people to Flycar "comfort" has special requirements, that is, the highest speed and the lowest speed during the ride the difference between the more comfortable ride (according to the speed from small to large sort, according to the greedy thought, find the smallest speed difference)

Code:

HDU 1598 #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 210#define INF    2147483646using namespace Std;int F[maxn], N, m, pos;struct edge{int u,v,val; friend bool operator< (const Edge&a,const edge&b) {return A.val < b.val;///by speed from small to large}} Arr[ma xn*maxn];///Initialization of void Init () {for (int i=0; i<maxn; ++i) f[i]=i;}    Look for root node (path compression) int find (int x) {if (f[x]==x) return x; else return F[x]=find (F[x]);}    Merge collection void Union (int x, int y) {int a=find (x), B=find (y);    if (a==b) return; F[b]=a;}    int main () {int u,v,speed,q; while (scanf ("%d%d", &n,&m)!=eof) {for (int i=0; i<m; i++) scanf ("%d%d%d", &arr[i].u,&amp        ; arr[i].v,&arr[i].val);        Sort (arr,arr+m);/////scanf ("%d", &q) from small to large            while (q--) {scanf ("%d%d", &u,&v);            int ans=inf; for (int j=0; j<m; J + +) {init ();//FirstStart, re-establish tree for (int k=j; k<m; k++) {Union (ARR[K].U,ARR[K].V);                        if (Find (U) ==find (v)) {ans = min (ans, arr[k].val-arr[j].val);                    Break            }}} if (Ans==inf) printf (" -1\n");        else printf ("%d\n", ans); }} return 0;}


HDU 1598 find the most comfortable road (minimum spanning tree)

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.