Tangled in Cables(Kruskal+map容器处理字符串)

/**

题意:

给你两个城市之间的道路(无向图),求出需要的

电缆。如果大于所提供的,就输出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;

}

时间: 2024-08-27 17:03:29

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

POJ 2075 Tangled in Cables (kruskal算法 MST + map)

Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6039   Accepted: 2386 Description You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start

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

http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in th

POJ 2075 Tangled in Cables (c++/java)

http://poj.org/problem?id=2075 题目大意: 给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求. 思路: 昨天20分钟的题,输入不小心写错了- -|||||看世界杯半场休息随便看了下发现了....T T 用map进行下标的映射,然后求MST即可. c++ #include<cstdio> #include<string> #include<map> #include<algorith

poj 2075 Tangled in Cables

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 6 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

关于map 容器insert顺序

今天测试我的节点,maya一次次死掉,一点一点的打印测试,良久才知:我想当然的将插入map的顺序,作为我执行的顺序直接遍历,打印数据显示,map有自动将键值排序的功能,比如以字符串为例,会按照a.b.c....顺序排好. 而且对于map,如果一个元素key不存在,但是直接map[key],那么map的size就增加1,我感觉有点儿相当于insert,但map[key]没有值.这样很不规范,对于有一定代码量的程序测试造成不必要的困扰,所以最好使用find函数,判断是否==map.end() 关于s

STL之map容器的详解

一.关于map的介绍 map是STL的 一个容器,和set一样,map也是一种关联式容器.它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键 字的值)的数据处理能力,由于这个特性,有助于我们处理一对一数据.这里说下map内部数据的组织,map内部是自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的.学习map我们一定要理解什么是一对一的数据映射?比如:一个班级中,每个学生的学号跟他的姓名

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

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为一个关键字对应一个值,输入格式如下: {a:3,b:4,c:10,f:6} {a:3,c:5,d:10,ee:4}冒号前面的表示关键字,冒号后面的数字表示值,关键字由小写字母组成.现在让你判断,如果新的字典相对于原来的字典有新增的关键字以以下格式输出 :+key1,key2,....如果新的字典相对

POJ 2075:Tangled in Cables 【Prim】

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so