HDU 4435(charge-station)(BFS+最小花费)

本文介绍了一种算法,用于解决在有限能量约束下,如何在多个城市间建立最少数量的加油站,确保能够遍历所有城市并返回起点的问题。该算法通过BFS遍历找到满足条件的最经济方案。

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

charge-station

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

There are n cities in M^3's empire. M^3 owns a palace and a car and the palace resides in city 1. One day, she wants to travel around all the cities from her palace and finally back to her home. However, her car has limited energy and can only travel by no more than D meters. Before it was run out of energy, it should be charged in some oil station. Under M^3's despotic power, the judge is forced to build several oil stations in some of the cities. The judge must build an oil station in city 1 and building other oil stations is up to his choice as long as M^3 can successfully travel around all the cities.
Building an oil station in city i will cost 2i-1 MMMB. Please help the judge calculate out the minimum cost to build the oil stations in order to fulfill M^3's will.

Input

There are several test cases (no more than 50), each case begin with two integer N, D (the number of cities and the maximum distance the car can run after charged, 0 < N ≤ 128).
Then follows N lines and line i will contain two numbers x, y(0 ≤ x, y ≤ 1000), indicating the coordinate of city i.
The distance between city i and city j will be ceil(sqrt((xi - xj)2 + (yi - yj)2)). (ceil means rounding the number up, e.g. ceil(4.1) = 5)

Output

For each case, output the minimum cost to build the oil stations in the binary form without leading zeros.
If it's impossible to visit all the cities even after all oil stations are build, output -1 instead.

Sample Input

3 3
0 0
0 3
0 1

3 2
0 0
0 3
0 1

3 1
0 0
0 3
0 1

16 23
30 40
37 52
49 49
52 64
31 62
52 33
42 41
52 41
57 58
62 42
42 57
27 68
43 67
58 48
58 27
37 69

Sample Output

11
111
-1
10111011

Hint

In case 1, the judge should select (0, 0) and (0, 3) as the oil station which result in the visiting route: 1->3->2->3->1. And the cost is 2^(1-1) + 2^(2-1) = 3.

分析:给你N个点,然后建立加油站,怎么建立加油站可以使这N个点都可以连一块(就是一个点可以到达任何一个点)

然后保证花的钱最小,按照给的顺序分别是1 2 3 4----N,每个建立的钱就是2的(i-1)次方。

最先开始BFS一遍,看看整个图是否都可以到达,因为有距离限制,然后从最后一个点枚举判断是否可以减去,最后从后边往前边输出,刚开始的0就不要了。

#include<stdio.h>
#include<iostream> 
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1)  return KGCD(a, b>>1);  if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);}  
int LCM(int a,int b){ return a/KGCD(a,b)*b; } 
int dir[5][2]={0,1,0,-1,1,0,-1,0};
using namespace std;
int a[200][2];
int map[200][200];
int map1[200];
int m,n;
int bfs()
{
	queue<int>q;
	int dis[200];
	int vis[200];
	for(int i=0;i<n;i++)
	{
		if(map1[i])		//默认距离  刚开始如果点可以加油的话就判断下到达这个点最短需要多少 
			dis[i]=0;
		else
			dis[i]=INF;
	}
	q.push(0);//将第一个放进去 每次都是第1个点开始起步,而且第一个点必须加油 
	memset(vis,0,sizeof(vis)); 
	vis[0]=1;
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		for(int i=0;i<n;i++)
		{
			if(!vis[i] && map[temp][i]<=m)//可以到达
			{
				dis[i]=min(dis[i],dis[temp]+map[temp][i]);//刷新下可以到i点的所有点中最短的那个距离 
				if(map1[i])//当前这个点可以作为加油站 
				{
					q.push(i);
					vis[i]=1;
				}
			//	printf("%d  %d   %d \n",temp,i,dis[i]);
			} 
		} 
	}
	for(int i=0;i<n;i++)
	{
		if(map1[i] && !vis[i])//这个点有   但是没能到达   
			return 0;
		if(!map1[i] && dis[i]*2>m)//这个地方不能加油,但是加完油回不来了 
			return 0; 
	}
	return 1;
}
void find()
{
	for(int i=n-1;i>0;i--)//开始枚举 判断是否可以去掉
	{
		map1[i]=0;
		if(!bfs())
			map1[i]=1;
	} 
	int temp=n-1;
	while(!map1[temp])//去掉那些多余的0
		temp--;
	for(int i=temp;i>=0;i--)
		printf("%d",map1[i]);
	printf("\n");
}
int main()
{

	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%d%d",&a[i][0],&a[i][1]);
		for(int i=0;i<n;i++)//计算距离  距离上取整 
		{
			for(int j=i+1;j<n;j++)
			{
				double temp=(double)sqrt((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1]));
				if(temp>int(temp))
					map[i][j]=map[j][i]=(int)temp+1;
				else
					map[i][j]=map[j][i]=(int)temp;
			}
		}
		memset(map1,0,sizeof(map1));
		for(int i=0;i<n;i++)
			 map1[i]=1;
		if(!bfs())//先跑一遍 看符合题意不 是否可以全部到达
		{
			printf("-1\n");
			continue;
		}
		else
			find();
	}
	return 0;
}

刚开始卡到了距离问题,怎么求任意两个点的最短,因为给的是随机的,不是按照顺序的大小,后来考虑先将所有可以设为加油点的所有点距离都是0,然后判断第一个点可以到的所有点,然后放到栈里面,这个时候这些点的距离还是0。

然后发现只有到那种去掉加油站的点的距离才是每两个点的距离,否则其余都是0。


总之就是只有是已经可以去掉的点,才可以刷出来最短距离,否则都是0,因为当作加油站点的没办法判断,因为在刚开始的BFS已经判断了所有的点可以在油用完之前到达。所以只有去掉某个点的时候,才会刷新出来到这个点的最短距离。



资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值