HDU 4463 Outlets

本文通过一个具体的购物商城实例,介绍了如何使用Kruskal算法来解决最小生成树问题,并对比了Prim算法的应用,旨在帮助读者理解这两种算法的区别及适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Outlets

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4182    Accepted Submission(s): 1911

 

 

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.

 

 

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

 

 

Source

2012 Asia Hangzhou Regional Contest

直接相连的两个店先设为0,最后再加上

kruskal 已ac

#include <iostream>
#include <string>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
#define maxx 100
using namespace std;
int pre[maxx],n,index,p,q;
struct r
{
	double x,y;
}rr[maxx];
struct Node
{
	double u,v,len;
}node[maxx*maxx];//边 
double dis(r a,r b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 
}
int cmp(Node a,Node b){
	return a.len<b.len;
}
void init(int n)//初始化 
{
	for(int i=1;i<=n;i++)
	{
		pre[i]=i;//自己的上级是自己 
	}
}
int find(int x)
{
	if(pre[x]==x)
	{
		return  x;
	}else return pre[x]=find(pre[x]);
}
void mix(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
	}
}
double kruskal()
{
	sort(node,node+index,cmp);
	init(n);
	double ans=0;
	ans=dis(rr[p],rr[q]);
	for(int i=0;i<index;i++)
	{
		Node e=node[i];
		if(find(e.u)!=find(e.v))
		{
			mix(e.u,e.v);
			ans+=e.len;
		}
	 } return ans;
}
int main()
{
	while(cin>>n,n)
	{
		cin>>p>>q;
		for(int i=1;i<=n;i++)
		{
			cin>>rr[i].x>>rr[i].y;
		}
		index=0;
		for(int i=1;i<=n;i++)//构造边。 
		{
			for(int j=i+1;j<=n;j++)
			{
				node[index].u=i;
				node[index].v=j;
				node[index].len=dis(rr[i],rr[j]);
				if((i==p&&j==q)||(i==q&&j==p))
				{
					node[index].len=0;
				 } 
				 index++;
			}
		}
		double ans=kruskal();
		cout<<fixed<<setprecision(2)<<ans<<endl;
	}return 0;
}
 

好气哦 别人写的prim 都是对的 就我一写就是错的 。。下面是我的未ac- pirm 先保存吧 有空看。。

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <cmath>
#define maxx 105
#define inf 0x3f3f3f3f
using namespace std; 
double dis[maxx],n,map[maxx][maxx];
int q,p;
int vis[maxx];
struct P{
	double x,y;
}node[maxx];
double Dis(P a,P b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 
}
double prim()
{
	memset(vis,0,sizeof(vis));
	memset(dis,inf,sizeof(dis));
	for(int i=1;i<=n;i++)
	{
		dis[i]=map[1][i];
	}
	vis[1]=1;
	dis[1]=0;
	double ans=0;
	for(int i=1;i<n;i++)
	{
		double minn=inf;
		int k=-1;
		for(int j=1;j<=n;j++)
		{
			if(!vis[j]&&dis[j]<minn)
			{
				minn=dis[j];
				k=j;
			}
		}
		vis[k]=1;
		ans+=minn;
		for(int l=1;l<=n;l++)
		{
			if(!vis[l]&&dis[l]>map[k][l])
			{
				dis[l]=map[k][l];
			}
		}
	}
	return ans;	
}
int main()
{
	while(cin>>n,n)
	{
		cin>>q>>p;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(i==j) map[i][j]=0;
				else map[i][j]=inf;
			}
		}
		for(int i=1;i<=n;i++)
		{
			cin>>node[i].x>>node[i].y;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
				map[i][j]=Dis(node[i],node[j]);
				if((i==p&&j==q)||(i==q&&j==p))
				map[i][j]=0;
			}
		}
		double ans=Dis(node[p],node[q]);
		cout<<fixed<<setprecision(2)<<ans+prim()<<endl;
	 } return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值