HDU-4463-Outlets

题目链接: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.
 

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 。。。 快哭了

#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;
 }

kruskal算法 

#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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值