poj2075
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 6348 | Accepted: 2505 |
Description
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
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
Source
POJ2075 Tangled in Cables 最小生成树
题目大意:
给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求。
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 15010
map<string,int>ad;
struct node{
int x,y;
double v;
node(int x=,int y=,double v=):x(x),y(y),v(v){}
}e[N];
int n,m,fa[N];double money;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool cmp(const node &a,const node &b){
return a.v<b.v;
}
int main(){
freopen("sh.in","r",stdin);
scanf("%lf%d",&money,&n);
for(int i=;i<=n;i++){
char str[];
scanf("%s",str);
ad[str]=i;
}
scanf("%d",&m);
for(int i=;i<=m;i++){
char c1[],c2[];
double cost;
scanf("%s %s %lf",c1,c2,&cost);
e[i].x=ad[c1];
e[i].y=ad[c2];
e[i].v=cost;
}
sort(e+,e+m+,cmp);
double ans=;int k=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++){
int fx=find(e[i].x),fy=find(e[i].y);
if(fx!=fy){
fa[fy]=fx;
ans+=e[i].v;
k++;
}
if(k==n-) break;
}
if(money>ans) printf("Need %.1lf miles of cable\n",ans);
else printf("Not enough cable\n");
return ;
}
poj2075的更多相关文章
- OJ题目分类
POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...
随机推荐
- python 时间 相关
http://www.jb51.net/article/47957.htm 不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部 ...
- 关于C++中_finite()函数的说明 [转]
The function int _finite(double x) returns 1 (true) if x is an ordinary number and 0 (false) if x is ...
- Http协议三次握手过程
Http协议三次握手过程 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...
- Storm文档详解
1.Storm基础概念 1.1.什么是storm? Apache Storm is a free and open source distributed realtime computation sy ...
- 不厚道一回->Omnifocus 2 for mac license
rt, 发个Omnifocus 2 for mac license. 其实Omnifocus 2的价格已经还算亲民了..可惜手贱一下子就找到了,所以没买了..不敢独享,所以分享给需要的人..有能力还是 ...
- CodeForces 659E New Reform
题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...
- 【Excle数据透视表】如何在组的顶部显示分类汇总
调整前 调整后 例 ...
- 微信小程序登录JAVA后台
代码地址如下:http://www.demodashi.com/demo/12736.html 登录流程时序登录流程时序 具体的登录说明查看 小程序官方API 项目的结构图: springboot项目 ...
- 在多线程的情况下是由Iterator遍历修改集合对象,报ConcurrentModificationException()异常的根因分析
遍历List时抛ConcurrentModificationException异常原理分析 http://www.blogjava.net/houlinyan/archive/2008/04/ ...
- MQTT--linux安装部署(CentOS)
OS环境:CentOS6.5 1.安装依赖 yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake ...