当前位置:网站首页>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;
}
边栏推荐
- 4274. Suffix expression - binary expression tree
- 453-atoi函数的实现
- 《重构:改善既有代码的设计》读书笔记(下)
- 股票证券公司排名,有安全保障吗
- Infix expression is converted to suffix expression (C language code + detailed explanation)
- 数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
- KT148A语音芯片使用说明、硬件、以及协议、以及常见问题,和参考代码
- 蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
- KT148A语音芯片ic的软件参考代码C语言,一线串口
- AcWing 1127. 香甜的黄油 题解(最短路—spfa)
猜你喜欢
Reading notes of "the way to clean structure" (Part 2)
定了,就是它!
自動生成VGG圖像注釋文件
Introduction to mongodb chapter 03 basic concepts of mongodb
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
Design and implementation of ks004 based on SSH address book system
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
KT148A语音芯片ic的开发常见问题以及描述
Kt148a voice chip IC user end self replacement voice method, upper computer
随机推荐
Zabbix5 client installation and configuration
Common problems and description of kt148a voice chip IC development
452-strcpy、strcat、strcmp、strstr、strchr的实现
Function high order curry realization
从20s优化到500ms,我用了这三招
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
Start practicing calligraphy
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
Implementation of online shopping mall system based on SSM
Reading notes of "the way to clean structure" (Part 2)
《MongoDB入门教程》第03篇 MongoDB基本概念
checklistbox控件用法总结
Cuckoo filter
浏览器缓存机制概述
安装单机redis详细教程
Golang:[]byte to string
函数高阶-柯里化实现
Introduction to program ape (XII) -- data storage
c语言里怎么设立优先级,细说C语言优先级