当前位置:网站首页>AcWing 1135. 新年好 题解(最短路+搜索)
AcWing 1135. 新年好 题解(最短路+搜索)
2022-07-02 18:27:00 【乔大先生】
AcWing 1135. 新年好
比较复杂的一道题,需要DFS+最短路,先用spfa找出六个起点到其余个点的最短路,再暴搜所有走亲戚的路线方案,找到最小值返回
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int>PII;
#define x first
#define y second
const int N = 5e4 + 10, M = 2e5 + 10, INF = 0x3f3f3f3f;
int n, m;
int h[N], ne[M], e[M], w[M], idx;
int dist[6][N]; //记录六个点(0+五个亲戚)作为起点到其余个点的最短路径
bool st[N];
int source[6];
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
int dfs(int u, int start, int dis){
//累积搜了u个亲戚,从第start个亲戚走来的,累积花费dis时间
if(u > 5) return dis; //如果已经搜完所有亲戚,直接返回结果
int res = INF; //初始化距离值
for(int i = 1; i <= 5; i ++ ){
if(!st[i]){
//如果这个亲戚没搜过,就搜索
int nest = source[i];
st[i] = true;
//dist[i][j]记录的是从第i个亲戚家出发到j的距离,所以start不用再转化
res = min(res, dfs(u + 1, i, dis + dist[start][nest]));
st[i] = false;
}
}
return res;
}
void spfa(int start, int dist[]){
memset(dist, 0x3f, N * 4);
dist[start] = 0;
memset(st, 0, sizeof st);
priority_queue<PII, vector<PII>, greater<PII>>heap; //用堆储存点和距离
heap.push({
0, start});
while(heap.size()){
auto op = heap.top();
heap.pop();
int an = op.y;
if(st[an]) continue; //如果这个点在堆内,就直接跳过
st[an] = true; //标记这个点在堆内
for(int i = h[an]; ~i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[an] + w[i]){
dist[j] = dist[an] + w[i];
heap.push({
dist[j], j});
}
}
}
}
int main()
{
cin>>n>>m;
source[0] = 1;
memset(h, -1, sizeof h);
for(int i = 1; i <= 5; i ++ ) cin>>source[i];
while(m -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for(int i = 0; i <= 5; i ++ ){
spfa(source[i], dist[i]);
}
memset(st, 0, sizeof st);
cout<<dfs(1, 0, 0)<<endl;
return 0;
}
边栏推荐
- Quanzhi A33 uses mainline u-boot
- 新手必看,点击两个按钮切换至不同的内容
- 【测试开发】软件测试—概念篇
- 以太网PHY层芯片LAN8720A简介
- 2022.7.1-----leetcode.241
- Data dimensionality reduction principal component analysis
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- zabbix5客户端安装和配置
猜你喜欢
![[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Novice must see, click two buttons to switch to different content

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

安装单机redis详细教程
Bubble sort array

高级性能测试系列《24. 通过jdbc执行sql脚本》

Compile oglpg-9th-edition source code with clion

IEDA refactor的用法

juypter notebook 修改默认打开文件夹以及默认浏览器
随机推荐
Reading notes of "the way to clean structure" (Part 2)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
电脑使用哪个录制视频软件比较好
Bubble sort array
PHP非对称加密方法私钥及公钥加密解密的方法
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
仿京东放大镜效果(pink老师版)
Yolov3 trains its own data set to generate train txt
Preprocessing and preprocessing macros
Gmapping code analysis [easy to understand]
虚拟机初始化脚本, 虚拟机相互免秘钥
数据降维——因子分析
新手必看,点击两个按钮切换至不同的内容
使用xml文件打印mybaties-log插件的方式
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
Golang concurrent programming goroutine, channel, sync
451-memcpy、memmove、memset的实现
What is the MySQL backup suffix_ MySQL backup restore
453-atoi函数的实现
ORA-01455: converting column overflows integer datatype