zoukankan      html  css  js  c++  java
  • HDOJ1162 Eddy's picture(最小生成树)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3746    Accepted Submission(s): 1844


    Problem Description
    Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
    Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
     
    Input
    The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

    Input contains multiple test cases. Process to the end of file.
     
    Output
    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
     
    Sample Input
    3 1.0 1.0 2.0 2.0 2.0 4.0
     
    Sample Output
    3.41

    #include<stdio.h>
    #include<math.h>

    struct node
    {
     float x;
     float y;
    }point[105];

    float G[105][105];

    float dis(int i,int j)
    {
     return (float)sqrt(pow((point[i].x-point[j].x),2)+pow((point[i].y-point[j].y),2));
    }

    float prim(int n)
    {
     int i,j,t,k;
     int visit[105];
     float min,len=0;
     float D[105];
     visit[0]=1;
     D[0]=0;
     for(i=1;i<n;++i)
     {
      D[i]=G[0][i];
      visit[i]=0;
     }
     for(i=1;i<n;++i)
     {
      k=0;
      min=(float)0x7fffffff;
      for(j=0;j<n;++j)
      {
       if(!visit[j]&&min>D[j])//注意这点与dijst求最短路径的不同
       {
        k=j;
        min=D[j];
       }
      }
      visit[k]=1;
      len+=min;
      for(t=0;t<n;++t)
       if(!visit[t]&&G[k][t]<D[t])
        D[t]=G[k][t];
     }
     return len;
    }

    int main()
    {
     int n,i,j;
     while(scanf("%d",&n)!=EOF)
     {
      for(i=0;i<n;++i)
       scanf("%f%f",&point[i].x,&point[i].y);
      for(i=0;i<n;++i)
       for(j=0;j<=i;++j)
        G[i][j]=G[j][i]=dis(i,j);
      printf("%.2f\n",prim(n));
     }
     return 0;
    }

    功不成,身已退
  • 相关阅读:
    使用IPTABLES限制IP上传下载速度,如何用iptables限速?
    基于queryperf 和 perftcpdns 的DNS压力测试
    搭建dnsmasq服务器,局域网内部解析
    Linux ipip隧道及实现
    使用FileZilla Server轻松搭建个人FTP服务器
    linux系统中如何查看日志 (常用命令)
    CentOS下安装XAMPP详细教程
    Tomcat中更改网站根目录
    php简单文件上传类
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2534451.html
Copyright © 2011-2022 走看看