【最小生成树专题】POJ 2075 Tangled in Cables

POJ 2075 Tangled in Cables

题目链接->http://poj.org/problem?id=2075

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6810 Accepted: 2661

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

Source

Mid-Atlantic 2004

Problem Idea

 【题意】
     题目理解:现在一个小镇要通过电缆来通电.给你小镇的房屋数目N和对应的名称,

共有M条边和这这M条边的边长,问你最少需要多少长的电缆才能让小镇所有房屋都通上电 即要求计算出必须连接所有房屋的最短电缆长度

  同时需要判断给定的最大电缆长度够不够用
 【类型】

  Kruskal算法求最小生成树

 【分析】
  Kruskal算法模板题,入门。

 【输入输出要求】

  输入输出要求,用中文复述一下:

  第一行给定一个实数,作为线轴上的最大电缆长度。
  第二行包含房屋的数量N,即图中的节点数量。
  接下来的N行给出了每个房屋所有者的名字。 每个名称最多由20个字符{a-z,A-Z,0-9}组成,不包含空格或标点符号。
  接下来一行:M,房屋之间的路径数量
  接下来的M行
  <房屋名称A> <房屋名称B> <距离>
  如果两个房子名称与上面列表中的两个不同名称匹配,并且距离为正实数。 在同一对房屋之间不会有两条路。
  输出将由一条线组成。 如果没有足够的电缆连接镇上的所有房屋,则输出:Not enough cable

  如果有足够的电缆,则输出 Need 10.2 miles of cable,保留到小数点后一位。
 【静态数组和动态数组的边列表的不同策略】

Edge edges[mmax];    //定义静态数组的边列表
    //vector<Edge> edges;//定义动态数组的边列表
 void AddEdge(int u,int v, double dist){
        edges[m++]=Edge(u,v,dist);
        //如若定义动态数组
        //edges.push_back(Edge(u,v,dist));
        //m=edges.size();
    }

Source Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cstdio>

using namespace std;
const int nmax=1000+10;
const int mmax=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;//无向图的点数n和边数m
    Edge edges[mmax];    //定义静态数组的边列表
    //vector<Edge> edges;//定义动态数组的边列表

    int fa[nmax];
    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);
        //如若定义动态数组
        //edges.push_back(Edge(u,v,dist));
        //m=edges.size();
    }
    double kruskal(){
        double sum=0;//最小生成树的边长之和
        int cnt=0;
        sort(edges,edges+m);

        for(int i=0;i<m;i++) {
            int u = edges[i].u;//得到第i条边的起点
            int v = edges[i].v;//得到第i条边的终点
            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;//当最小生成树不存在时,返回-1
        return sum;//如若最小生成树存在,返回边长之和

    }
}KK;
int main() {
    double sum_max;//可用电缆的最大长度
    int n,m;//无向图的点数n和边数m
    map<string,int>mp;//指定房屋所有者的名字与节点的编号的映射
    scanf("%lf\n%d",&sum_max,&n);
    KK.init(n);
    for(int i=0;i<n;i++){//初始化n个几点
        string s;
        cin>>s;//房屋所有者的名字;即图中节点的名字
        mp[s]=i;//根据房屋所有者的名字指定该节点的编号
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        string s1,s2;
        double d;
        cin>>s1>>s2>>d;//输入变得起点、终点、边长
        KK.AddEdge(mp[s1],mp[s2],d);//向图中增加一条边
    }
    double ans=KK.kruskal();
    if(ans==-1 || ans>sum_max) printf("Not enough cable\n");
    else printf("Need %.1f miles of cable\n",ans);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值