当前位置:网站首页>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;
}
边栏推荐
- PHP非对称加密方法私钥及公钥加密解密的方法
- 以太网PHY层芯片LAN8720A简介
- 安装单机redis详细教程
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- Thread application instance
- 云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
- Progress progress bar
- Advanced performance test series "24. Execute SQL script through JDBC"
- IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
- 新手必看,点击两个按钮切换至不同的内容
猜你喜欢

Watchful pioneer world outlook Architecture - (how does a good game come from)

Machine learning notes - time series prediction research: monthly sales of French champagne

Processing strategy of message queue message loss and repeated message sending

Refactoring: improving the design of existing code (Part 1)

ICDE 2023|TKDE Poster Session(CFP)

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

【测试开发】软件测试—概念篇

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
随机推荐
Quanzhi A33 uses mainline u-boot
MySQL高级(进阶)SQL语句
[test development] software testing - concept
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
新手必看,點擊兩個按鈕切換至不同的內容
C文件输入操作
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
Progress progress bar
【测试开发】软件测试—概念篇
How to print mybats log plug-in using XML file
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
《代码整洁之道》读书笔记
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
《架构整洁之道》读书笔记(下)
股票证券公司排名,有安全保障吗
性能测试如何创造业务价值
Data dimensionality reduction principal component analysis
Excel finds the same value in a column, deletes the row or replaces it with a blank value
线程应用实例