hdu 4463 Outlets(prim)

此题为一典型最小生成树问题,要求在确保Nike店与Apple店直接相连的前提下,构建连接所有店铺的路径,使得总路径长度最短。采用Prim算法进行求解。

Outlets

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


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
 

解题报告:

终于把一道区域赛的水题给A了,现在在学最小生成树的prim算法,真心表示没有kruskal好写,但毕竟是图论的一个算法,说不准什么时候有道卡数据的题优化一下可以过呢!

这道题首先说了nike 和apple必须直接相连,然后这个就相当于prim算法的第一步已经预先执行过了,根据prim算法的执行过程,下一步是找最段的距离,因此先在执行一下a,端点,再找b端点的最小值,取二者较小即可;


附加数据一组:

5
2 3
0 0
1 1
1 -1
-1 -1
-1 1

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,a,b,vis[210];
double mp[105][105];
const int INF = 0x7fffffff;
struct Point
{
	int x,y;
}pt[105];

double prim()
{
	int cnt;
	double ans,lowcost[105],min;
	vis[a]=1,vis[b]=1;
	ans += mp[a][b];
	for(int i=1;i<=n;i++)
		lowcost[i]=mp[a][i];
	for(int j=1;j<=n;j++)
	{
		if(lowcost[j]>mp[b][j])
			lowcost[j]=mp[b][j];
	}
	for(int i=1;i<n;i++)
	{
		min=1.0*INF,cnt=-1;
		for(int j=1;j<=n;j++)
		{
			if(!vis[j]&&min>lowcost[j])
			{
				min=lowcost[j];
				cnt=j;
			}
		}
		//printf("_cnt = %d,min=%d\n",cnt,min);
		if(cnt == -1) return ans;
			ans+=min;
			vis[cnt]=1;
		for(int j=1;j<=n;j++)
		{
			if(!vis[j]&&lowcost[j]>mp[cnt][j])
				lowcost[j]=mp[cnt][j];
		}
	}

	printf("%.2lf\n",ans);
}

int main()
{
	while(scanf("%d",&n),n)
	{
		scanf("%d%d",&a,&b);
		for(int i=1;i<=n;i++)
			scanf("%d%d",&pt[i].x,&pt[i].y);
		for(int i=1;i<=n;i++)
		{
			mp[i][i]=INF;
			for(int j=i+1;j<=n;j++)
			{
				mp[i][j]=mp[j][i]=(double)sqrt(1.0*(pt[i].x -pt[j].x)*(pt[i].x-pt[j].x )+1.0*(pt[i].y-pt[j].y )*(pt[i].y -pt[j].y ));
				//printf("mp[%d][%d]=%.2lf       ",i,j,mp[i][j]);
			}
			//printf("\n");
		}
		//
		memset(vis,0,sizeof(vis));
		printf("%.2lf\n",prim());
	}
	return 0;
} 





内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值