ACM HDU 1162 Eddy's picture(간단 한 최소 생 성 트 리,템 플 릿 사용 연습 시작)

12715 단어 최소 생 성 트 리
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2454    Accepted Submission(s): 1184
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
 
Author
eddy
 
Recommend
JGShining
 
/*
HDU1162 Eddy's picture
: n , ,
*/
#include
<stdio.h>
#include
<math.h>
#include
<string.h>
#define MAXN 110
struct Node//
{
double x,y;
}node[MAXN];
double cost[MAXN][MAXN];//
double distance(Node a,Node b)//
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//***********************************************************************
// prim
//***********************************************************************
#define typec double
const typec INF=0x3f3f3f3f;
int vis[MAXN];
typec lowc[MAXN];
typec prim(typec cost[][MAXN],
int n)// 0~n-1
{
int i,j,p;
typec minc,res
=0;
memset(vis,
0,sizeof(vis));
vis[
0]=1;
for(i=1;i<n;i++) lowc[i]=cost[0][i];
for(i=1;i<n;i++)
{
minc
=INF;
p
=-1;
for(j=0;j<n;j++)
if(vis[j]==0&&minc>lowc[j])
{minc
=lowc[j];p=j;}
if(minc==INF)return -1;//
res+=minc;vis[p]=1;
for(j=0;j<n;j++)
if(vis[j]==0&&lowc[j]>cost[p][j])
lowc[j]
=cost[p][j];
}
return res;
}
//********************************************************************
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf(
"%lf%lf",&node[i].x,&node[i].y);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)cost[i][j]=0;
else
cost[i][j]
=distance(node[i],node[j]);
}
printf(
"%.2lf
",prim(cost,n));
}
return 0;
}

좋은 웹페이지 즐겨찾기