您的位置:首页 > 编程语言

哈理工acm练习赛 K - Tangled in Cables

2015-11-01 19:36 501 查看
K - Tangled in Cables
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld
& %llu
SubmitStatusPracticeZOJ
2326

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�Cz,A�CZ,0�C9} 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 + mst

/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-11-01 19:35
#
# Filename: A.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
const int M=1000000;
int F[M];
int r[M];
int n,m,t,cn;
double res,ans;

struct edge
{
int u,v; double w;
} per[M];

bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void init()
{
for(int i=1; i<=n; i++)
{
F[i]=i;
r[i]=1;
}
}

//int Find(int x)
//{
// if(x!=F[x])
// F[x]=Find(F[x]);
// return F[x];
//}
int Find(int x)
{
int r=x;
while(r!=F[r])
{
r=F[r];
}
int k=x;
while(k!=r)
{
int t=F[k];
F[k]=r;
k=t;
}
return r;
}
bool union_set(int u,int v)
{
int tx=Find(u);
int ty=Find(v);
if(tx==ty)
return false;
else if(r[tx]>r[ty])
F[ty]=tx;
else if(r[tx]<r[ty])
F[tx]=ty;
else
{
F[tx]=ty;
r[ty]++;
}
return true;
}

int main()
{
double sum;
while(cin>>sum)
{
map<string,int>cnt;
int t=1;
int ans=0;
string str;
init();
scanf("%d", &n);
for(int i=0; i<n; i++)
{
cin>>str;
cnt[str]=t++;
}
scanf("%d", &m);
for(int i=0; i<m; i++)
{
string str2;
double x;
cin>>str>>str2>>x;
per[i].u = cnt[str];
per[i].v = cnt[str2];
per[i].w = x;
}
sort(per,per+m,cmp);
init();
cn=0;
res=0;
for(int i=0; i<m; i++)
{
if(union_set(per[i].u,per[i].v))
{
cn++;
res+=per[i].w;
if(cn==n-1)
break;

}
}
if(res > sum){
cout<<"Not enough cable"<<endl;
}
else{
printf("Need %.1lf miles of cable\n", res);
}
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程 最小生成树 acm