1. 程式人生 > >poj2075 最小生成樹(Kruskal模板)

poj2075 最小生成樹(Kruskal模板)

Description

You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start your business properly and are relying on parts you have found in an old warehouse you bought. Among your finds is a single spool of cable and a lot of connectors. You want to figure out whether you have enough cable to connect every house in town. You have a map of town with the distances for all the paths you may use to run your cable between the houses. You want to calculate the shortest length of cable you must have to connect all of the houses together.

Input

Only one town will be given in an input. 

  • The first line gives the length of cable on the spool as a real number. 
  • The second line contains the number of houses, N 
  • The next N lines give the name of each house's owner. Each name consists of up to 20 characters {a–z,A–Z,0–9} and contains no whitespace or punctuation. 
  • Next line: M, number of paths between houses 
  • next M lines in the form


< house name A > < house name B > < distance > 
Where the two house names match two different names in the list above and the distance is a positive real number. There will not be two paths between the same pair of houses.

Output

The output will consist of a single line. If there is not enough cable to connect all of the houses in the town, output 
Not enough cable 
If there is enough cable, then output 
Need < X > miles of cable 
Print X to the nearest tenth of a mile (0.1).

Sample Input

100.0
4
Jones
Smiths
Howards
Wangs
5
Jones Smiths 2.0
Jones Howards 4.2
Jones Wangs 6.7
Howards Wangs 4.0
Smiths Wangs 10.0

Sample Output

Need 10.2 miles of cable

現在一個小鎮要通過電纜來通電.給你小鎮的房屋數目N和對應的名稱,已經它們之間的距離.問你最少需要多少長的電纜才能讓小鎮所有房屋都通上點.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<iostream>
using namespace std;
const int maxn=1000+10;
const int maxm=1000*1000+10;
 
struct Edge
{
    int u,v;
    double dist;
    Edge(){}
    Edge(int u,int v,double d):u(u),v(v),dist(d){}
    bool operator<(const Edge&rhs)const
    {
        return dist <rhs.dist;
    }
};
 
struct Kruskal
{
    int n,m;
    Edge edges[maxm];
    int fa[maxn];
    int findset(int x){ return fa[x]==-1? x: fa[x]=findset(fa[x]); }
 
    void init(int n)
    {
        this->n=n;
        m=0;
        memset(fa,-1,sizeof(fa));
    }
 
    void AddEdge(int u,int v,double dist)
    {
        edges[m++]=Edge(u,v,dist);
    }
 
    double kruskal()
    {
        double sum=0;
        int cnt=0;
        sort(edges,edges+m);
 
        for(int i=0;i<m;i++)
        {
            int u=edges[i].u, v=edges[i].v;
            if(findset(u) != findset(v))
            {
                sum += edges[i].dist;
                fa[findset(u)] = findset(v);
                if(++cnt>=n-1) break;
            }
        }
        if(cnt<n-1) return -1;
        return sum;
    }
}KK;
 
int main()
{
    map<string,int> mp;
    double max_sum;
    int n,m;
    scanf("%lf%d",&max_sum,&n);
    KK.init(n);
    for(int i=0;i<n;i++)
    {
        string s;
        cin>>s;
        mp[s]=i;
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        string s1,s2;
        double x;
        cin>>s1>>s2>>x;
        KK.AddEdge(mp[s1],mp[s2],x);
    }
    double ans = KK.kruskal();
    if(ans==-1 || ans> max_sum) printf("Not enough cable\n");
    else printf("Need %.1f miles of cable\n",ans);
    return 0;
}