/**
题意:
    给你两个城市之间的道路(无向图),求出需要的
    电缆。如果大于所提供的,就输出Not enough 。。。
    否则输出所需要的电缆长度。
 
    输入:N (给定的电缆总长度)
          m1 (有多少个城市—)
          str1
          str2
          str3
          str4
          :
          ;(城市的名字)
 
          m2(相当于给出m2条边)
          a  b  c (a城市到b城市的距离为c)
          :
          :
          :
          :
    输出:
         所需的最短长度   若大于给定的长度  输出 Not enough cable
 
分析: 简单的最小生成树+字符串处理
        用map容器映射处理简单
 
 
*/
 
 
#include<stdio.h>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int MAX=1e4;///1*10 的4次方
int vest[MAX];
int n,m;
double leng=0;
map<string,int >mp;
struct node
{
    int u,v;
    double w;
} bian[MAX];
bool  cmp(node a,node b)
{
    return a.w<b.w;
}
void init(int n)
{
    for(int i=0; i<=n; i++)
        vest[i]=i;
}
int Find(int t)
{
        if(vest[t]==t)return t;
     return  Find(vest[t]);
 
}
bool merge(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(x!=y)
    {
        vest[x]=y;
        return true;
    }
    return false;
}
double Kruskal()
{
    double sum=0;
    int cnt=0;
    for(int i=0; i<m; i++)
    {
        if(merge(bian[i].u,bian[i].v))
        {
            sum+=bian[i].w;
            ++cnt;
        }
        if(cnt>=n-1)break;
    }
    return sum;
}
int main()
{
    scanf("%lf",&leng);
    {
 
        mp.clear();
        scanf("%d",&n);
        init(n);
        char a[25];
        int k=1;
        for(int i=0; i<n; i++)
        {
            scanf("%s",a);
            if(mp[a]==0)mp[a]=k++;///给第一次出现的城市编号
        }
        scanf("%d",&m);
        char b[25];
        double c;
        for(int i=0; i<m; i++)
        {
            scanf("%s%s%lf",a,b,&c);
            bian[i].u=mp[a];
            bian[i].v=mp[b];
            bian[i].w=c;
        }
        sort(bian,bian+m,cmp);
        double sum=Kruskal();
        if(sum>=leng)printf("Not enough cable\n");
        else printf("Need %.1lf miles of cable\n",sum);
    }
    return 0;
}

Tangled in Cables(Kruskal+map容器处理字符串)的更多相关文章

  1. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  2. map容器

    map容器一般用于对字符串进行编号,主要用于建图方面,例如把城市名按数字进行编号 #include"stdio.h" #include"string.h" #i ...

  3. 小白鼠排队(map容器插入数据的四种方法)

    题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...

  4. BestCoder Round #92 1001 Skip the Class —— 字典树 or map容器

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1001 题解: 1.trie树 关 ...

  5. HDU 1113 Word Amalgamation (map 容器 + string容器)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...

  6. matlab map容器类型

    matlab map容器类型 map容器类型以及map类概述 map是将一个量映射到另一个量上,此是前面的量就是map的键(key),后面的量就是map的数据(value).map的键和对应的数据都储 ...

  7. map 容器的使用

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map> ...

  8. 一种map容器遍历的方法

    遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...

  9. stl之map容器的原理及应用

    容器的数据结构同样是采用红黑树进行管理,插入的元素健位不允许重复,所使用的节点元素的比较函数,只对元素的健值进行比较,元素的各项数据可通过健值检索出来.map容器是一种关联容器,实现了SortedAs ...

随机推荐

  1. eclipse代码注释的设置

    http://blog.csdn.net/shiyuezhong/article/details/8450578 1. eclipse用户名的设置: 在eclipse的安装路径下,打开eclipse. ...

  2. c# 左连接写法

    var itemandformulas = from i in AttendanceItemList join f in AttendanceFormulaList on i.AttendanceCo ...

  3. How to Make LastPass Even More Secure with Google Authenticator

    Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...

  4. svn的初级使用

    首先呢 你需要下载一个软件  比如说是 Cornerstone. 进行安装好之后 然后 然后输入账号密码 就可以了 然后去xcode去进行相关的配置 点击第二个进入 偏好设置 点击最下边的+ 点击第二 ...

  5. sql server主动推送客户端更新数据

    小谈需求: 最近工作上接到一个需求,做一个web展示数据的报表,最好能实时更新,不限制所用技术. 第一个问题:web服务器推送给浏览器新数据,一开始我想到的最快的最简单的方法就是 在web页面上js轮 ...

  6. 哈哈,CSDN又支持Windows Live Writer了

    从10年开始写CSDN博客,后面不支持WLW了,就不怎么写了,话说自带的编辑器确实不怎么样,不过又支持了,那就哈哈,重新开工了. 关于如何配置的,跟以前一样,详情如下所示: http://blog.c ...

  7. window下配置SSH连接GitHub、GitHub配置ssh key(转)

    转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...

  8. Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  9. Centos JAVA Eclipse

    wget http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-i586.tar.gz vi /etc/profile 在 ...

  10. HTML5画布