当前位置:网站首页>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;
}
边栏推荐
- Correspondence between pytoch version, CUDA version and graphics card driver version
- AcWing 1128. 信使 题解(最短路—Floyd)
- mysql函数
- Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
- Chapter 7 - class foundation
- KT148A语音芯片ic的硬件设计注意事项
- Golang concurrent programming goroutine, channel, sync
- VBScript详解(一)
- KT148A语音芯片ic的开发常见问题以及描述
- What is the MySQL backup suffix_ MySQL backup restore
猜你喜欢

Zabbix5 client installation and configuration

Design and implementation of ks004 based on SSH address book system

MySQL function

搭建哨兵模式reids、redis从节点脱离哨兵集群

Embedded (PLD) series, epf10k50rc240-3n programmable logic device

Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1

Implementation of online shopping mall system based on SSM

AcWing 1126. Minimum cost solution (shortest path Dijkstra)

Build a master-slave mode cluster redis

Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
随机推荐
Notes de lecture sur le code propre
Yes, that's it!
Golang concurrent programming goroutine, channel, sync
基于SSM实现网上购物商城系统
mysql函数
JS how to get integer
SQLite 3.39.0 release supports right external connection and all external connection
AcWing 1125. 牛的旅行 题解(最短路、直径)
《重构:改善既有代码的设计》读书笔记(下)
Refactoring: improving the design of existing code (Part 1)
KT148A语音芯片使用说明、硬件、以及协议、以及常见问题,和参考代码
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
Golang并发编程——goroutine、channel、sync
Refactoring: improving the design of existing code (Part 2)
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
AcWing 383. 观光 题解(最短路)
zabbix5客户端安装和配置
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Dictionaries
AcWing 1125. Cattle travel problem solution (shortest path, diameter)