Tangled in Cables
Time Limit: 1000MS | Memory Limit: 30000K |
Total Submissions: 4841 | Accepted: 1947 |
题目连接:http://poj.org/problem?id=2075
Description
You are the owner of SmallCableCo and have purchased thefranchise rights for a small town. Unfortunately, you lack enough funds tostart your business properly and are relying on parts you have found in an oldwarehouse you bought. Among your finds is a single spool of cable and a lot ofconnectors. You want to figure out whether you have enough cable to connectevery house in town. You have a map of town with the distances for all thepaths you may use to run your cable between the houses. You want to calculatethe shortest length of cable you must have to connect all of the housestogether.
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 thedistance is a positive real number. There will not be two paths between thesame pair of houses.
Output
The output will consist of a single line. If there is notenough 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
解题思路:
根据输入建立结构体来存储每一个单词,和对应的id,根据下面输入,用单词的id来建图,在用prim算法来计算最小生成树,最后比较得出的结果是否大于给定长度。
代码:
#include <iostream>
#include <cstdio>
#include<cstring>
#define maxn 1000
#define VALUE 0xffffff
using namespace std;
struct trieWord
{
char words[22];
int id;
};
trieWord trie[maxn];
double g[maxn][maxn];
double dis[maxn];
int visited[maxn];
double length;
int n,m;
//求最小生成树
double prim()
{
int i;
for(i=1;i<=n;i++)
{
visited[i]=0;
dis[i]=VALUE;
}
dis[1]=0;//从任意一点出发
double res=0;
while(true)
{
int t=-1;
for(i=1;i<=n;i++)
{
if(visited[i]==0 && (t==-1 || (dis[i]<dis[t] && dis[i]!=0)))
t=i;
}
if(t==-1)
break;
visited[t]=1;
res+=dis[t];
for(i=1;i<=n;i++)
{
if(dis[i]>g[i][t] && g[i][t]!=0)
{
dis[i]=g[i][t];
}
}
}
return res;
}
int getId(char *ch)
{
for(int i=1;i<=n;i++)
{
if(strcmp(ch,trie[i].words)==0)
return trie[i].id;
}
return 0;
}
int main()
{
int i;
scanf("%lf",&length);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",trie[i].words);
trie[i].id=i;
}
scanf("%d",&m);
char ch1[22],ch2[22];
double count;
for(i=1;i<=m;i++)
{
scanf("%s %s %lf",ch1,ch2,&count);
int id1=getId(ch1);
int id2=getId(ch2);
g[id1][id2]=count;
g[id2][id1]=count;
}
double res=prim();
if(res>length)
{
printf("Not enough cable\n");
}
else
{
printf("Need %.1f miles of cable\n",res);
}
return 0;
}