Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11983 Accepted Submission(s): 6017 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.
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.
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
|
题意:连接n个点的最小长度。
思路:经典prim()算法,套模板。
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
double x;
double y;
}s[105];
int n;
double mapp[105][105];
int vis[105];
double dis[105];
double del(int a,int b)
{
return sqrt( pow((s[a].x-s[b].x),2)+pow((s[a].y-s[b].y),2) );
}
void prim(int a)
{
double sum=0;
vis[a]=1;
dis[a]=0;
for(int i=1;i<n;i++)
dis[i]=mapp[i][a];
for(int i=0;i<n-1;i++)
{
double minn=inf;
int l;
for(int j=0;j<n;j++)
{
if(!vis[j]&&minn>dis[j])
{
minn=dis[j];
l=j;
}
}
sum+=minn;
vis[l]=1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&dis[j]>mapp[l][j])
dis[j]=mapp[l][j];
}
}
cout<<fixed<<setprecision(2)<<sum<<endl;
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>s[i].x>>s[i].y;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
mapp[i][j]=mapp[j][i]=0;
else
mapp[i][j]=mapp[j][i]=del(i,j);
}
}
memset(vis,0,sizeof(vis));
prim(0);
}
return 0;
}