﻿HDU 1162 Eddy’s picture(Prime最小生成树)-Ocrosoft

# Eddy’s picture

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

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

```

Solution

```#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 100+10
using namespace std;
int n,m;
double mp[MAXN][MAXN],lowcast[MAXN];
bool vis[MAXN];
struct Point
{
double x,y;
double dis(Point p)
{return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));}
};
double prime(int u0)
{
double ans=0;int pos=u0;//pos记录为起点
ms(vis),vis[u0]=1;//清空vis数组,标记起点
for(int i=1; i<=n; i++)//初始化lowcast数组
if(i!=pos)lowcast[i]=mp[pos][i];
for(int i=1; i<n; i++)
{
double minn=INF;
for(int j=1;j<=n;j++)//找到没访问过的而且最近的邻近点
if(!vis[j]&&minn>lowcast[j])minn=lowcast[j],pos=j;
if(minn==INF)return 0;
ans+=minn;
vis[pos]=1;//标记这个邻近点
for(int j=1;j<=n;j++)//更新lowcast数组
if(!vis[j]&&lowcast[j]>mp[pos][j])lowcast[j]=mp[pos][j];
}
return ans;
}
int main()
{
while(cin>>n&&n)
{
vector<Point> v;
Point t;
v.push_back(t);
for(int i=1;i<=n;i++)
{
cin>>t.x>>t.y;
v.push_back(t);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)mp[i][j]=0;
else mp[i][j]=v[i].dis(v[j]);
}
}
printf("%.2lf\n",prime(1));
}
return 0;
}
```