杭电1162 Eddy's picture( 最小生成树)

http://acm.hdu.edu.cn/showproblem.php?pid=1162

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

这个题将各个点之间的距离存入edge数组中,利用prim做就行

附AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
#define INF 0x3f3f3f3f
double edge[maxn][maxn],dist[maxn]={0},visit[maxn];
int n,m;
double sum;
struct point{
  double x,y;
}p[110];
double prim(int cur){
    int index=cur;
    memset(visit,false,sizeof(visit));
    for(int i=1;i<=n;i++)
        dist[i]=edge[cur][i];
    visit[cur]=true;
    for(int i=1;i<n;i++){
        double minx=INF;
        for(int j=1;j<=n;j++){
            if(!visit[j]&&minx>dist[j]){
                minx=dist[j];
                index=j;
            }
        }
        visit[index]=true;
        //printf("%d ",index);
        sum+=minx;
        for(int i=1;i<=n;i++){
            if(!visit[i]&&dist[i]>edge[index][i]){
                dist[i]=edge[index][i];
            }
        }
    }
    return sum;
}
int main()
{
    while(~scanf("%d",&n)){
            memset(edge,INF,sizeof(edge));
            sum=0.0;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                if(i==j)
                edge[i][j]=INF;
             else edge[i][j]=sqrt((abs(p[i].x-p[j].x)*abs(p[i].x-p[j].x))+abs(p[i].y-p[j].y)*abs(p[i].y-p[j].y));
            }
            printf("%.2f\n",prim(1));
    }
}

猜你喜欢

转载自blog.csdn.net/curry___/article/details/82390699
今日推荐