当前位置:网站首页>AcWing 1135. Happy New Year (shortest path + search)
AcWing 1135. Happy New Year (shortest path + search)
2022-07-02 19:43:00 【Mr. Qiao Da】
AcWing 1135. Happy New Year
A complicated problem , need DFS+ shortest path , First use spfa Find the shortest path from six starting points to the other points , Then search all the route plans of relatives , Find the minimum value and return
#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]; // Record six points (0+ Five relatives ) As the shortest path from the starting point to the other points
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){
// Accumulated search u A relative , From start A relative came , Cumulative expenses dis Time
if(u > 5) return dis; // If you have searched all your relatives , Direct return
int res = INF; // Initialize the distance value
for(int i = 1; i <= 5; i ++ ){
if(!st[i]){
// If this relative hasn't searched , Just search
int nest = source[i];
st[i] = true;
//dist[i][j] The record is from i Relatives left for j Distance of , therefore start No more conversion
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; // Stack storage points and distances
heap.push({
0, start});
while(heap.size()){
auto op = heap.top();
heap.pop();
int an = op.y;
if(st[an]) continue; // If this point is in the pile , Just skip it
st[an] = true; // Mark this point in the heap
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;
}
边栏推荐
- AcWing 1137. 选择最佳线路 题解(最短路)
- Refactoring: improving the design of existing code (Part 1)
- AcWing 383. 观光 题解(最短路)
- AcWing 1128. 信使 题解(最短路—Floyd)
- Istio部署:快速上手微服务,
- Common problems and description of kt148a voice chip IC development
- AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
- MySQL table historical data cleaning summary
- Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
- 字典
猜你喜欢

Py's interpret: a detailed introduction to interpret, installation, and case application

Istio1.12: installation and quick start

KT148A语音芯片ic的软件参考代码C语言,一线串口

Detailed tutorial on installing stand-alone redis

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

蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么

安装单机redis详细教程

浏览器缓存机制概述

Introduction to program ape (XII) -- data storage

Yes, that's it!
随机推荐
End to end object detection with transformers (Detr) paper reading and understanding
程序猿入门攻略(十二)——数据的存储
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
checklistbox控件用法总结
MySQL table historical data cleaning summary
多端小程序开发有什么好处?覆盖百度小程序抖音小程序微信小程序开发,抢占多平台流量红利
Common problems and description of kt148a voice chip IC development
AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
mysql函数
rxjs Observable 自定义 Operator 的开发技巧
From 20s to 500ms, I used these three methods
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
RPD product: super power squad nanny strategy
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Cuckoo filter
Detailed tutorial on installing stand-alone redis
NMF-matlab