当前位置:网站首页>【acwing】1135. 新年好***(dijkstra+堆优化)
【acwing】1135. 新年好***(dijkstra+堆优化)
2022-04-22 15:55:00 【percation】


预处理,再搜索出最小的答案。
m l o g n mlog_n mlogn + 5 ! 5! 5!
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int,int> pii;
const int N = 5e4 + 10, M = 4e5 + 10, INF = 0x3f3f3f3f;
int n,m;
int source[6];
int h[N],e[M],w[M],ne[M],idx;
int q[N],dis[6][N];
bool st[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void heap_dij(int start, int dis[]){
memset(dis,0x3f, N * 4);//之所以不用sizeof(dis),因dis此时为二维数组
dis[start] = 0;
memset(st,0,sizeof(st));
priority_queue<pii, vector<pii>,greater<pii>> heap;
heap.push({
0,start});
while(heap.size()){
pii t = heap.top();
heap.pop();
int ver = t.second;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; i!= -1; i = ne[i]){
int j = e[i];
if(dis[j] > dis[ver] + w[i]){
dis[j] = dis[ver] + w[i];
heap.push({
dis[j],j});
}
}
}
}
int dfs(int u, int start, int dist){
if(u == 6) return dist;
int res = INF;
for(int i = 1; i <= 5; i++){
//五个亲戚
if(!st[i]){
int next = source[i];
st[i] = true;
res = min(res,dfs(u + 1,i,dist + dis[start][next]));
st[i] = false;
}
}
return res;
}
int main(){
scanf("%d%d",&n,&m);
source[0] = 1;
memset(h,-1,sizeof(h));
for(int i = 1; i <= 5; i++) scanf("%d",&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 < 6; i++){
heap_dij(source[i],dis[i]);
}
memset(st,0,sizeof(st));
printf("%d\n",dfs(1,0,0));//拜访第一个亲戚,当前起点为source0,总距离0.
return 0;
}
版权声明
本文为[percation]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45866781/article/details/124343688
边栏推荐
- Weekly recommended short video: how to reconstruct the core competitiveness of enterprises in the stock era?
- Grafana series (XIII): how to use Loki collection to view kubernetes events
- 【虹科技术分享】ntopng是如何进行攻击者和受害者检测
- Shell脚本中sed使用
- SQL statement - Multi - table Associated Query
- 想做自媒体运营却不会写作?4个珍藏的运营技巧
- Alibaba P9 handwritten 39 module redis core notes. I had a successful interview and got a salary increase of 7K
- 设某二叉树采用二叉链表表示(即结点的两个指针分别指示左、右子树),当该二叉树包含k个结点时,其二叉链表结点中必有( )个空的左右指针。
- Tencent cloud fortress machine opens OTP authentication
- [model] state space average modeling - step down
猜你喜欢

报名开启|QKE 容器引擎托管版暨容器生态发布会!

Redis simple storage folder

Altium designer除了GND以外的Nets自动布线
![BrokenPipeError: [Errno 32] Broken pipe](/img/75/c0e2f82aa0222a3374fb6c1ffc2778.png)
BrokenPipeError: [Errno 32] Broken pipe

建筑业未来的发展方向:数字化工厂管理系统

这个API Hub厉害了,收录了钉钉企业微信等开放Api,还能直接调试 !

ML之FE:结合Kaggle比赛的某一案例细究特征工程(Feature Engineering)思路框架

SQL statement - Multi - table Associated Query

生物素-4-荧光素实验注意事项

每周推荐短视频:存量时代如何重构企业核心竞争力?
随机推荐
342-Leetcode 字符串中的第一个唯一字符
Grafana series (IX): introduction to Loki, an open source cloud native logging solution
【虹科技术分享】ntopng是如何进行攻击者和受害者检测
一文学会JVM性能优化
单选按钮选中
Shell脚本尝试
思科交换机配置
Dataset之CASIA-WebFace:CASIA-WebFace 数据集的简介、安装、使用方法之详细攻略
OSPF的详细讲解、分类,还有实验讲解
Greenplum【代码分享 01】实现replace insert或insert on conflict类似on duplicate key update批量入库数据(合并插入无则新增有则更新)
短信平台API接口demo示例-Node/SMS/Send
BrokenPipeError: [Errno 32] Broken pipe
Clothing private domain + supply chain management
一个页面向同一个地方提交两个form表单
LeetCode每日一题——824. 山羊拉丁文
Frequently asked questions about recent BSN development
【鲲鹏训练营】重庆2022开发者大赛
RF of mL: the kaggle competition uses the Titanic data set to establish an RF model to predict whether everyone is rescued or not
性能飙升66%的秘密:AMD 2.5万元768MB 3D缓存霄龙首次开盖
Redis optimization series (I) building redis master-slave based on docker