P2935 Best Spot

P2935 Best Spot

题目描述

现在有n个地点,其中f个是贝茜所喜欢的,给出m条路,求出一个到所有贝茜喜欢的地点的距离之和最小的地点

思路

因为我们要统计出每个节点到喜欢的节点的距离,所以要求出任意两个点之间的最短距离,可以用Floyd来做,复杂度 O ( n 3 ) O(n^3) O(n3),由于n很小,所以可以承受。接下来我们就需要统计出每个节点到喜欢的节点的总距离,可以用 O ( n f ) O(nf) O(nf)枚举求出答案,总时间复杂度 O ( n 3 ) O(n^3) O(n3)

贴代码

#include<cstdio>
#include<iostream>
#include<cstring>
#define maxn 600
using namespace std;
long long map[maxn][maxn];
long long p,f,c;
long long l[maxn];
int main(){
	cin>>p>>f>>c;
	for(long long i=1;i<=f;i++){
		cin>>l[i];
	}
	for(int i=1;i<=p;i++){
		for(int j=1;j<=p;j++){
			map[i][j]=0x3fffffff; 
		}
	}
	long long x,y,z; 
	for(long long i=1;i<=c;i++){
		cin>>x>>y>>z;
		map[x][y]=min(map[x][y],z);
		map[y][x]=map[x][y];
	}
	for(long long i=1;i<=p;i++){
		map[i][i]=0;
	}
	for(long long i=1;i<=p;i++){
		for(long long j=1;j<=p;j++){
			for(long long k=1;k<=p;k++){
				map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
			}
		}
	}
	long long num=22222222222222;
	long long id=0;
	for(long long i=1;i<=p;i++){
		long long ans=0;
		for(long long j=1;j<=f;j++){
			ans+=map[l[j]][i];
		}
		if(num>ans){
			num=ans;
			id=i;
		}
	}
	cout<<id;
} 
以下是用模拟退火算法实现对一天行程的活动推介的 Python 代码: ```python import random import math # 定义景点数据 scenic_spots = [ {"name": "景点A", "x": 1, "y": 1, "time": 2}, {"name": "景点B", "x": 2, "y": 2, "time": 3}, {"name": "景点C", "x": 3, "y": 3, "time": 1}, {"name": "景点D", "x": 4, "y": 4, "time": 2}, {"name": "景点E", "x": 5, "y": 5, "time": 1}, {"name": "景点F", "x": 6, "y": 6, "time": 2}, {"name": "景点G", "x": 7, "y": 7, "time": 3}, {"name": "景点H", "x": 8, "y": 8, "time": 1}, {"name": "景点I", "x": 9, "y": 9, "time": 2}, {"name": "景点J", "x": 10, "y": 10, "time": 1} ] # 计算两个景点之间的距离 def distance(spot1, spot2): return math.sqrt((spot1["x"]-spot2["x"])**2 + (spot1["y"]-spot2["y"])**2) # 计算一条路线的总体验和时间 def calc_route(route): total_time = 0 total_exp = 0 for i in range(len(route)-1): spot1 = scenic_spots[route[i]] spot2 = scenic_spots[route[i+1]] total_time += distance(spot1, spot2) total_time += spot1["time"] total_exp += 1 return total_exp, total_time # 模拟退火算法 def simulated_annealing(init_route, init_temp, min_temp, alpha): current_route = init_route current_exp, current_time = calc_route(current_route) best_route = current_route best_exp, best_time = current_exp, current_time while init_temp > min_temp: for i in range(100): new_route = current_route.copy() # 随机交换两个景点的位置 r1 = random.randint(0, len(new_route)-1) r2 = random.randint(0, len(new_route)-1) new_route[r1], new_route[r2] = new_route[r2], new_route[r1] new_exp, new_time = calc_route(new_route) delta_exp = new_exp - current_exp delta_time = new_time - current_time # 计算接受新状态的概率 if delta_exp > 0: p = 1 else: p = math.exp(delta_exp / init_temp) if random.random() < p: current_route, current_exp, current_time = new_route, new_exp, new_time if current_exp > best_exp: best_route, best_exp, best_time = current_route, current_exp, current_time init_temp *= alpha return best_route, best_exp, best_time # 初始化路线 init_route = list(range(len(scenic_spots))) random.shuffle(init_route) # 设置初始温度、最小温度和温度衰减系数 init_temp = 100 min_temp = 1 alpha = 0.99 # 运行模拟退火算法 best_route, best_exp, best_time = simulated_annealing(init_route, init_temp, min_temp, alpha) # 输出结果 print("最佳路线为:", [scenic_spots[i]["name"] for i in best_route]) print("总体验值为:", best_exp) print("总时间为:", best_time) ``` 在以上代码中,我们定义了 `scenic_spots` 数组来存储景点数据,每个景点包括名称、横纵坐标和游览时间。我们使用 `distance` 函数计算两个景点之间的距离,使用 `calc_route` 函数计算一条路线的总体验和时间。在 `simulated_annealing` 函数中,我们使用模拟退火算法来寻找最佳路线。初始路线采用随机排列的方式生成,然后在每个温度下进行 100 次交换两个景点位置的操作,计算交换后的总体验和时间,根据概率公式判断是否接受新状态,最终得到最佳路线和总体验值和总时间。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值