Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7397 Accepted Submission(s): 3753
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?
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.
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
kruskal+prime
kruskal算法:
//kruskal
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define max 100*50+10
using namespace std;
int set[110];
struct line
{
int start;
int end;
double dis;
}num[max];
bool cmp(line a,line b)
{
return a.dis<b.dis;
}
int find(int p)
{
int child=p;
int t;
while(p!=set[p])
p=set[p];
while(child!=p)
{
t=set[child];
set[child]=p;
child=t;
}
return p;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
double need,a[110],b[110];//a,b分别表示横 纵坐标
int point,road;
int i,j,x,y,t;
while(scanf("%d",&point)!=EOF)
{
for(i=1;i<=point;i++)
{
set[i]=i;
scanf("%lf %lf",&a[i],&b[i]);
}
t=0;
for(i=1;i<=point;i++)
{
for(j=i+1;j<=point;j++)
{
num[t].start=i;
num[t].end=j;
num[t].dis=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
t++;
}
}
sort(num,num+t,cmp);
need=0;
for(i=0;i<t;i++)
{
if(find(num[i].start)!=find(num[i].end))
{
merge(num[i].start,num[i].end);
need+=num[i].dis;
}
}
printf("%.2lf\n",need);
}
return 0;
}
prime算法:
//prime
#include<stdio.h>
#include<math.h>
#define INF 0x3f3f3f
#define max 100+10
int visit[max];
double low[max],map[max][max];
int point;
void prime()
{
int i,j,next;
double min,mincost=0;
for(i=1;i<=point;i++)
{
low[i]=map[1][i];
visit[i]=0;
}
visit[1]=1;
for(i=2;i<=point;i++)
{
min=INF;
for(j=1;j<=point;j++)
{
if(!visit[j]&&min>low[j])
{
min=low[j];
next=j;
}
}
visit[next]=1;
mincost+=min;
for(j=1;j<=point;j++)
{
if(!visit[j]&&low[j]>map[next][j])
{
low[j]=map[next][j];
}
}
}
printf("%.2lf\n",mincost);
}
int main()
{
int i,j;
double a[max],b[max];
while(scanf("%d",&point)!=EOF)
{
for(i=1;i<=point;i++)
{
for(j=1;j<=point;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
}
for(i=1;i<=point;i++)
{
scanf("%lf %lf",&a[i],&b[i]);
}
for(i=1;i<=point;i++)
{
for(j=i+1;j<=point;j++)
{
map[i][j]=map[j][i]=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
}
}
prime();
}
return 0;
}