现在的位置: 首页 > 综合 > 正文

poj2075Tangled in Cables (最小生成树之prim)

2018年02月22日 ⁄ 综合 ⁄ 共 2250字 ⁄ 字号 评论关闭
Problem 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 {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 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
这题目有点坑,就在输出把%.1lf改成%.1f就过了……。
#include<stdio.h>
#include<string.h>
double edg[505][505],sum,SUM,node[505],INF=1000000.0;
int s[505];
void set_first(int n)
{
    for(int i=1;i<=n;i++)
    {
        s[i]=0; node[i]=INF;
        for(int j=i+1;j<=n;j++)
        edg[j][i]=edg[i][j]=INF;
    }
}
int Prim(int n,int m)
{
    int tm=m;
    double min;
    s[m]=1;sum=0.0;
    for(int k=2;k<=n;k++)
    {
        min=INF;
        for(int i=1;i<=n;i++)
        if(s[i]==0)
        {
            if(node[i]>edg[tm][i])
            node[i]=edg[tm][i];
            if(min>node[i])
            {
                min=node[i];  m=i;
            }
        }
        s[m]=1; sum+=min; tm=m;
    }
    if(SUM>=sum)
    return 1;
    return 0;
}
int main()
{
    char str[10000][25] ,name1[25],name2[25];
    int n,m,flog,k,x,y;
    double c;
    scanf("%lf%d",&SUM,&n);
    for(int i=1;i<=n;i++)
        scanf("%s",str[i]);
        set_first(n);
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
      scanf("%s %s %lf",name1,name2,&c);
        for(int j=1;j<=n;j++)
        if(strcmp(str[j],name1)==0)
            x=j;
        else if(strcmp(str[j],name2)==0)
            y=j;
        if(edg[x][y]>c)
        edg[x][y]=edg[y][x]=c;
    }
    flog=Prim(n,1);
    if(flog)
    printf("Need %.1f miles of cable\n",sum);
    else
    printf("Not enough cable\n");
}

抱歉!评论已关闭.