poj 2075 Tangled in Cables【最小生成树+字符串处理】
来源:互联网 发布:淘宝安佳旗舰店假货 编辑:程序博客网 时间:2024/05/09 16:28
Tangled in Cables
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 6213 Accepted: 2449
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.
< 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.
- 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).
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.04JonesSmithsHowardsWangs5Jones Smiths 2.0Jones Howards 4.2Jones Wangs 6.7Howards Wangs 4.0Smiths Wangs 10.0
Sample Output
Need 10.2 miles of cable
最小生成树,这道题不算难,主要是处理的技巧,如果直接暴力编号的话,也可以过,不过可以用map,这样更方便一些,虽然牺牲了点时间,map,具体我也不太懂,,想了解详情推荐百度一下.....
map,只是为了编号而已,暴力也行,后面的处理就是kruscal 算法了,按常规的思路一步步做而已,注意根据题意输出就行,注意数据类型的控制....
#include<stdio.h>#include<algorithm>#include<string>#include<map>using namespace std;int n,m,per[1005];double s;struct lu{int a,b;double len;}x[1005];int cmp(lu a,lu b){return a.len<b.len;}void init(){for(int i=0;i<n;++i){per[i]=i;}}int find(int x)//查找{int r=x;while(r!=per[r]){r=per[r];}int i=x,j;while(i!=r){j=per[i];per[i]=r;i=j;}return r;}int join(int x,int y)//合并{int fx=find(x),fy=find(y);if(fx!=fy){per[fy]=fx;return 1;}return 0;}void kruscal()//操作{init();int i,cnt=0;double sum=0;for(i=0;cnt<n-1;++i){if(join(x[i].a,x[i].b)){sum+=x[i].len;//统计花费++cnt;}}if(sum>s){printf("Not enough cable\n");return;}printf("Need %.1lf miles of cable\n",sum);}int main(){int i,j;double len;char a[25],b[25];while(~scanf("%lf",&s)){map<string,int> map;scanf("%d",&n);//按要求输入for(i=0;i<n;++i){getchar();scanf("%s",a);map[a]=i;}scanf("%d",&m);for(i=0;i<m;++i){getchar();scanf("%s%s%lf",a,b,&len);x[i].a=map[a];x[i].b=map[b];//记录边的具体情况x[i].len=len;}sort(x,x+m,cmp);//排序kruscal();}return 0;}
0 0
- poj 2075 Tangled in Cables【最小生成树+字符串处理】
- POJ 2075 Tangled in Cables(最小生成树)
- poj 2075 -- Tangled in Cables(最小生成树)
- POJ 2075 Tangled in Cables(最小生成树)
- POJ 2075 Tangled in Cables 最小生成树
- POJ 2075 Tangled in Cables 最小生成树 Kruskal && Prim
- POJ 2075 Tangled in Cables(最小生成树 kruscal)
- POJ 2075 Tangled in Cables
- poj 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- poj 2075 Tangled in Cables
- poj 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- poj 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- zoj 2326 Tangled in Cables 最小生成树
- poj2075 Tangled in Cables 最小生成树+map,kruskal
- springMVC 笔记
- light oj1074
- VC6 破解Windows登录密码(不支持64位)
- CentOS 5.5使用yum来安装LAMP(php运行环境)
- 十年只是刹那间
- poj 2075 Tangled in Cables【最小生成树+字符串处理】
- [人月神话]读书笔记6--永恒的变化&&开发工具效用
- mybatis学习笔记---mapper标签及其子标签的属性
- [Java Concurrency in Practice]第五章 基础构建模块
- 直流减速电机带磁电 编码器
- Android API的View类
- linux下的输出重定向和快捷键
- 【数据结构】线性表顺序结构的操作---C/C++语言
- 【数据结构】双向循环线性表的基本操作--C++/C实现