poj2075并查集加堆实现的克鲁…

原题:

Description

You are the owner of SmallCableCo and havepurchased the franchise rights for a small town. Unfortunately, youlack enough funds to start your business properly and are relyingon parts you have found in an old warehouse you bought. Among yourfinds is a single spool of cable and a lot of connectors. You wantto figure out whether you have enough cable to connect every housein town. You have a map of town with the distances for all thepaths you may use to run your cable between the houses. You want tocalculate the shortest length of cable you must have to connect allof the houses together.

Input

Only one town will be given in aninput. 
  • The first line gives the length of cable on the spool as a realnumber. 
  • The second line contains the number of houses,N 
  • The next N lines give the name of each house's owner. Each nameconsists of up to 20 characters {a–z,A–Z,0–9} and contains nowhitespace or punctuation. 
  • Next line: M, number of paths betweenhouses 
  • next M lines in the form

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

Output

The output will consist of a single line. If thereis 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 ofcable 
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个房屋之间铺设缆线,要求总长度尽量短;
思路:裸的最小生成树,目测房屋所构的图比较稀疏,用并查集加堆容易写。由于输入的是字符串,所以用map将字符串和房屋序号绑定,利于以后代码的计算。。。。
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stdio.h>
#define inf 0x6FFFFFFF

using namespace std;
struct point
{
    int x,y;
    double distance;
};
bool operator<(point a,point b)
{
    returna.distance>b.distance;
}
priority_queue<point> que;
map<string ,int > maps;
double sum;
int parent[500];
int n;
int getparent(int k)
{
    if(parent[k]==k)
    return k;
    returnparent[k]=getparent(parent[k]);
}
int main()
{
    scanf("%lf",&sum);
    scanf("%d",&n);
    for(inti=0;i<n;i++)
    {
        string s;
        cin>>s;
        maps.insert(pair<string,int>(s,i));
        parent[i]=i;
    }
    int m;
    scanf("%d",&m);
    for(inti=0;i<m;i++)
    {
        string s1,s2;
        double di;
        cin>>s1>>s2>>di;
        point p;
        p.x=maps.find(s1)->second;
        p.y=maps.find(s2)->second;
        p.distance=di;
        que.push(p);
    }
    double ans=0;
    for(inti=1;i<n;i++)
    {
        while(!que.empty()&&getparent(que.top().x)==getparent(que.top().y))
          que.pop();
        point p=que.top();
        ans+=p.distance;
        parent[getparent(p.x)]=getparent(p.y);
    }
    if(ans>sum)
    printf("Not enough cable\n");
    else
    printf("Need %.1lf milesof cable\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值