1. 程式人生 > >HDU 1162 Eddy's picture (最小生成樹 prim)

HDU 1162 Eddy's picture (最小生成樹 prim)

坐標 .cn for paper cati contain discover NPU first

題目鏈接

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<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
struct Node
{
    double x;
    double y;
} node [101];
double tu[101][101],sum;//圖,存儲的是任意的兩點之間的距離
double dis[101];//到該點的最短距離
int vis[101],n;//標記數組,標記一個點是否已經放到最小生成樹的集合中
double fun(const Node a,const Node b)//求兩點之間的距離
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}


void prim()//裸的prim算法
{
    double  Min;
    sum=0.0;
    int k;
    //默認以0點作為最小生成樹的起點
    for(int i=0; i<n; i++)
    {
        dis[i]=tu[0][i];//保存0點到任一點的距離
        vis[i]=0;
    }
    vis[0]=1;//0這個點已經放到最小生成樹裏面
    for(int i=1; i<n; i++)//構建n-1條邊就行了
    {
        Min=0x3f3f3f3f;
        for(int j=0; j<n; j++)//找到最小生成樹之外的最小的那條邊
        {
            if(vis[j]==0&&dis[j]<Min)
            {
                Min=dis[j];
                k=j;
            }
        }
        //if(Min==0x3f3f3f3f)
        //  break;
        vis[k]=1;
        sum+=Min;
        //printf("%.2lf\n",sum);
        for(int j=0; j<n; j++)
        {
            if(vis[j]==0&&dis[j]>tu[k][j])
                dis[j]=tu[k][j];
        }
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%lf%lf",&node[i].x,&node[i].y);
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                tu[i][j]=0x3f3f3f3f;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(i==j) continue;
                else tu[i][j]=min(tu[i][j],fun(node[i],node[j]));
            }
        }
        prim();
        printf("%.2lf\n",sum);
    }
    return 0;
}

HDU 1162 Eddy's picture (最小生成樹 prim)