Tangled in Cables
Time Limit: 1000MS | Memory Limit: 30000K | |
Total 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.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,这样更方便一些,虽然牺牲了点时间,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;
}