题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4463
Outlets
Problem Description
In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
Input
There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.
Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
Sample Input
4 2 3 0 0 1 0 0 -1 1 -1 0
Sample Output
3.41
prim 算法 把一定要在一起的两个点的距离设为0,sum把他们的距离加进来就好了 ,一定注意double 。。。
![快哭了](http://static.blog.csdn.net/xheditor/xheditor_emot/default/fastcry.gif)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,u,v,w,p,q;
int visit[111];
double e[111][111];
double dis[111];
const double inf=999999;
void init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
e[i][j]=0;
else
e[i][j]=inf;
}
}
}
void prim()
{
int i,j,k;
memset(visit,0,sizeof(visit));
double minn,sum=0.0;
sum=sum+e[p][q];
e[p][q]=e[q][p]=0;
for(i=1;i<=n;i++)
{
dis[i]=e[1][i];
}
visit[1]=1;
int count=1;
while(count<n)
{
minn=inf;
for(i=1;i<=n;i++)
{
if(visit[i]==0&&dis[i]<minn)
{
minn=dis[i];
j=i;
}
}
visit[j]=1;
count++;
sum=sum+dis[j];
for(k=1;k<=n;k++)
{
if(visit[k]==0&&dis[k]>e[j][k])
dis[k]=e[j][k];
}
}
printf("%.2lf\n",sum);
}
int main()
{
int i,j;
double x[111],y[111];
while(~scanf("%d",&n))
{
if(n==0)
break;
scanf("%d%d",&p,&q);
init();
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
int m=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
e[i][j]=e[j][i]=ww;
}
}
prim();
}
return 0;
}
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int pre[111];
int n,p,q;
struct edge{
int u,v;
double w;
}e[11111];
void init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int r)
{
if(pre[r]==r)
return r;
else
{
pre[r]=find(pre[r]);
return pre[r];
}
}
int merge(int u,int v)
{
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
pre[t2]=t1;
return 1;
}
return 0;
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int main()
{
int i,j;
double x[111],y[111];
while(cin>>n)
{
if(n==0)
break;
cin>>p>>q;
for(i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
}
init();
int m=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
e[m].u=i;
e[m].v=j;
e[m].w=ww;
m++;
}
}
pre[p]=q;
double sum=sqrt((x[p]-x[q])*(x[p]-x[q])+(y[p]-y[q])*(y[p]-y[q]));
sort(e+1,e+m,cmp);
int count=1;
for(i=1;i<=m;i++)
{
if(merge(e[i].u,e[i].v))
{
count++;
sum+=e[i].w;
}
if(count==n-1)
break;
}
printf("%.2lf\n",sum);
}
return 0;
}