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

XX Star There are many cities, these exchanges between cities through a strange highway SARS (Super Air Roam Structure --- Super Air roaming structure), each of SARS are limited to a fixed Speed of travel above Flycar, while XX stars who have special requirements for "comfort level" flycar, and that is the difference between the maximum speed during the ride and the lowest speed of the smaller more comfortable ride, (understood as the speed requirements of SARS, flycar must instant speed / slow down, the pain ah) ,
but XX star who did not so much of the time requirements. You want to find the most comfortable path between a city. (SARS is bidirectional).

 

Input

Input comprises a plurality of test instances, each comprising:
a first line has two positive integers n (1 <n <= 200 ) , and m (m <= 1000), expressed the M and N cities SARS.
The next three lines are positive integers StartCity, EndCity, speed, to represent the surface StartCity EndCity, speed limit speedSARS. speed <= 1000000
and is a positive integer Q (Q <11), indicates the number of pathfinding.
Next Q lines each have two positive integers Start, End, indicates the starting end pathfinding. Output wayfinding requirements of each line of print, only the output of a non-negative integer comfort best route maximum speed difference between the lowest speed. If the start and end points can not 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 




algorithm
of this question a maximum minimum maximum minimum
1 spanning half 2.
get half should have decided
but not feel half
so consider the spanning tree
because the sorted sequence is monotonically increasing
so that each selection from an edge it began to enumerate run spanning
the last when the start and end points in the set which ended
with a final plus of minus side edge of the current enumeration

correctness proof
if the current side is not in the minimum spanning tree inside
so than the spanning tree inside edge the current large side
so when I went on to enumerate the difference getting smaller and smaller


code:
//
#include<bits/stdc++.h>
using namespace std;
#define maxnn 100000
int n,m;
int Q;
int tot;
int f[maxnn];
struct  node 
{
    int en,le;
    int sta;
}tree[maxnn];
void add(int a,int b,int c)
{
        tree[++tot].en=b;
        tree[tot].sta=a;
        tree[tot].le=c;
}
bool cmp(node a,node b)
{
    return a.le<b.le;
}
int gf(int v)
{
     return f[v]==v? v: f[v]=gf(f[v]);
}
void kruskal(int a,int b)
{
    
    int ans=1000000000;
    for(int i=1;i<=tot;i++)
    {
            for(int z=1;z<=n;z++)
        {
            f[z]=z;
        }
        for(int j=i;j<=tot;j++)
        if(gf(tree[j].sta)!=gf(tree[j].en))
        {
            f[gf(tree[j].sta)]=gf(tree[j].en);
            if(gf(a)==gf(b))
            {
                ans=min(ans,tree[j].le-tree[i].le);
                break;
            }
        }
    }
    if(ans==1000000000) 
    cout<<-1<<endl;
    else
    cout<<ans<<endl;
}
int main()
{
    int a,b,c;
    int st,e;
    while(cin>>n>>m)
{
    tot=0;
        for(int i=1;i<=m;i++)
    {
        cin>>a>>b>>c;
        add(a,b,c);
        add(b,a,c);
    }
    sort(tree+1,tree+1+tot,cmp);
    cin>>Q;
    
    for(int i=1;i<=Q;i++)
    {
        for(int i=1;i<=n;i++)
        {
            f[i]=i;
        }
        cin>>st>>e;
        kruskal(st,e);
    }
    
}
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11260346.html