HDU—4463 Outlets 最小生成树
Posted jkzr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU—4463 Outlets 最小生成树相关的知识,希望对你有一定的参考价值。
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.
InputThere 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.
OutputFor 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
题意:给你n个点,最小的价值使得所有的点连通,但是p,q一定是直连的。
这是一道比较的模板的最小生成树的题,但是要保证有一条边一定在这颗树内,我们可以使用Kruskal算法的时候,直接把ans先设置为p,q之间距离的值,然后在加边的时候把值设置为0,那么根据Kruskal算法的思想,
这个边最小肯定是最先加进来了,那么其他的就和其他的没有区别了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=105;
int n,p,q;
int cnt;
struct Point
{
int x,y;
}point[maxn];
struct Node
{
int from,to;
double value;
}node[maxn*maxn];
int fa[maxn];
bool cmp(Node a,Node b)
{
return a.value<b.value;
}
void init()
{
for(int i=0;i<maxn;i++)
fa[i]=i;
}
int findd(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=findd(fa[x]);
}
double getdist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Kruskal()
{
double ans=getdist(point[p],point[q]);
for(int i=1;i<=cnt;i++)
{
int fx=findd(node[i].from);
int fy=findd(node[i].to);
if(fx!=fy)
{
ans+=node[i].value;
fa[fx]=fy;
}
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
scanf("%d %d",&p,&q);
for(int i=1;i<=n;i++)
scanf("%d %d",&point[i].x,&point[i].y);
cnt=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cnt++;
node[cnt].from=i;node[cnt].to=j;
node[cnt].value=getdist(point[i],point[j]);
if((i==p&&j==q)||(i==q&&j==p))
node[cnt].value=0;
}
}
init();
sort(node+1,node+cnt+1,cmp);
double sum=Kruskal();
printf("%.2f
",sum);
}
return 0;
}
以上是关于HDU—4463 Outlets 最小生成树的主要内容,如果未能解决你的问题,请参考以下文章