POJ 2075 Tangled in Cables
来源:互联网 发布:淘宝假发店铺推荐知乎 编辑:程序博客网 时间:2024/05/18 03:23
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
本来是一道简单的最小生成树的题,然而,也不难。
建立一个映射,从名字到编号(map)。
题目似乎没有给数据范围,我就开到了2000。
#include<algorithm>#include<iostream>#include<cstdio>#include<string>#include<map>using namespace std;const int maxn=2005;map<string,int>mx;int n,m,tp,f[maxn];double l,ans;string s;struct node{ int x,y; double val;}a[maxn*maxn];bool cmp(node c,node d){ return c.val<d.val;}int fnd(int x){ if(f[x]!=x) f[x]=fnd(f[x]); return f[x];}int main(){ scanf("%lf%d",&l,&n); for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=n;i++) { cin>>s; mx[s]=++tp; } scanf("%d",&m); for(int i=1;i<=m;i++) { cin>>s; a[i].x=mx[s]; cin>>s; a[i].y=mx[s]; scanf("%lf",&a[i].val); } sort(a+1,a+m+1,cmp); for(int i=1;i<=m;i++) if(fnd(a[i].x)!=fnd(a[i].y)) { f[fnd(a[i].y)]=fnd(a[i].x); ans+=a[i].val; } if(ans>l) printf("Not enough cable\n"); else printf("Need %.1f miles of cable\n",ans); return 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
- poj 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- POJ 2075 Tangled in Cables
- POJ 2075 Tangled in Cables(Kruskal算法)
- POJ 2075 Tangled in Cables (c++/java)
- poj -- 2075 Tangled in Cables (mst)
- POJ 2075:Tangled in Cables 【Prim】
- 2075:Tangled in Cables
- POJ 2075 Tangled in Cables(最小生成树)
- zoj 2326 || poj 2075 Tangled in Cables(MST)
- poj 2075 -- Tangled in Cables(最小生成树)
- POJ 2075 Tangled in Cables(最小生成树)
- NYOJ995-经典DP
- 常用排序算法的性能分析及应用场景
- 聊聊并发(1):深入分析Volatile的实现原理
- 斜率优化,数形结合(Average,uva 1451)
- ARC下OC对象和CF对象之间的桥接(bridge)
- POJ 2075 Tangled in Cables
- Linux-C基础知识学习:C语言作业-输入n,打印n行*,每行*数与当前行数相等
- 基于Android平台的流媒体播放器的设计
- ios app提交之前需要哪几个证书
- poj 1961 Period
- linux&unix find命令
- android布局layout中的一些属性
- scala 学习笔记(三)
- iOS开发之检查更新