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. 
  • 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.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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微乐贵阳捉鸡麻将不显示头像怎么办 苹果6p用久了卡怎么办 小猪民宿平台限制三天上线怎么办 交易猫买家申请了仲裁我的钱怎么办 雷达石英手表表里面有小灰尘怎么办 电脑宽带用户名和密码忘记了怎么办 腾讯会员不让别人在异地登录怎么办 中毒后电脑文件后缀是lnk怎么办 wps逗号隔的空不一样大怎么办 电脑盘里的文件删不了怎么办 在淘宝买的qq账号被找回了怎么办 微信公众号的密码忘了怎么办 公众号安全助手密码忘了怎么办 微博账号存在发布违规信息怎么办 余额宝转出到银行卡被冻结怎么办 银行账户被冻结被转出钱怎么办 从余额宝转出的资金被冻结怎么办 微信账号卖了但是实名认证了怎么办 uc下载文档里的文档全没了怎么办 二手乐视没有账号和密码怎么办 华为账号密码忘了手机卡丢了怎么办 联想平板微信更新后不可兼容怎么办 小米手机刷完机账号密码忘了怎么办 红米手机的小米账号密码忘了怎么办 小米手机账号密码手机号忘了怎么办 小米手机忘了账号和密码怎么办 自己的小米账号密码忘了怎么办 小米手机丢了不记得小米账号怎么办 小米手环账号密码忘了怎么办 阴阳师一个区的账号找不到了怎么办 阴阳师手机账号代练登录了后怎么办 我的微信账号被盗更改密码了怎么办 vivo手机密保密码忘了怎么办 华为手机保密柜忘记密保问题怎么办 支付宝账号突然说没有了怎么办 快手号密码可能被盗登不上该怎么办 海岛奇兵小米版换手机了怎么办 申诉找回微信密码验证吗错误怎么办 微信密码忘了申诉不成功怎么办 微信密码忘了申诉不了怎么办 微信密码忘了申诉不回来怎么办