POJ 2075 Tangled in cables

Source: Internet
Author: User

Tangled in cables time limit:2000/1000ms (java/other) Memory limit:60000/30000k (Java/other) total submission (s): 19 Accepted Submission (s): 6Problem Descriptionyou is the owner of Smallcableco and has purchased the franchise rights For a small the town. Unfortunately, lack enough funds to start your business properly and is relying on parts we have found in a old war Ehouse you bought. Among your finds is a single spool of cable and a lot of connectors. You want-to-figure out whether the enough cable to connect every house in town. You had a map of town with the distances for all the paths and you could use the run your cable between the houses. You want to calculate the shortest length of cable you must has to connect all of the houses together.
Inputonly one town would be given on 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 characters {az,az,09} 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 house names match, different names in the list above and the distance are a positive real number. There'll is not paths between the same pair of houses.
Outputthe output would 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 and then output
Need < X > miles of Cable
Print X to the nearest tenth of a mile (0.1).
Sample Input
100.04jonessmithshowardswangs5jones Smiths 2.0Jones Howards 4.2Jones wangs 6.7Howards wangs 4.0Smiths wangs 10.0

Sample Output
Need 10.2 miles of cable This problem is the smallest tree deformation, the name of the transformation into a point, the smallest tree to beg! Of course there is a length requirement! Code:
#include <stdio.h> #include <string.h> #define INF 0xfffffff#define N 1010char s[10000][30];d ouble Map[n][n] , Cost[n],m,outcome;int country,road,v[n];void Prim () {int i,j,next;double mincost;memset (v,0,sizeof (v)); for (i=0;i    <country;i++) {Cost[i]=map[0][i]; } v[0]=1;  for (i=1;i<country;i++)//Find the shortest distance {mincost=inf;   for (j=0;j<country;j++) {if (!v[j]&&mincost>cost[j]) {mincost=cost[j];   Next=j;}   } outcome+=mincost;   V[next]=1;  for (j=0;j<country;j++)//update path {if (!v[j]&&cost[j]>map[next][j]) {cost[j]=map[next][j];}} }}int Main () {int i,j;double L;char a[30],b[30];while (scanf ("%lf", &m)!=eof) {scanf ("%d", &country); for (i=0;i < country;i++) scanf ("%s", S[i]); scanf ("%d", &road);   for (i=0;i<country;i++) {for (j=0;j<country;j++) {if (i==j) map[i][j]=0;   else Map[i][j]=inf;  }} for (i=0;i<road;i++)//character converted to point {scanf ("%s%s%lf", a,b,&l);  int k,t; for (j=0;j<country;j++) {if (strcmp (s[j],a) ==0) k=j;   } for (j=0;j<country;j++) {if (strcmp (s[j],b) ==0) t=j;  } map[t][k]=map[k][t]=l;  } prim ();  if (outcome<=m) printf ("Need%.1f miles of cable\n", outcome); else printf ("Not Enough cable\n"); return 0;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2075 Tangled in cables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.