当前位置:网站首页>HDU 2680 shortest path Dijkstra + chained forward star + priority queue (template)
HDU 2680 shortest path Dijkstra + chained forward star + priority queue (template)
2022-04-22 13:15:00 【@Li Sicheng】
sketch :
It is more convenient to build a map in reverse , Finally, compare the path from the end point to the starting point , Choose the shortest one , Is the answer .
subject :http://acm.hdu.edu.cn/showproblem.php?pid=2680
Code :
#include <iostream>
#include <queue>
#include <vector>
#define MAX 0x3f3f3f3f
const int maxn = 2e4+10;
using namespace std;
struct Edge{
int to,dis,next;
};
Edge e[maxn];
int head[maxn],dis[maxn],cnt;
int vis[maxn];
void add_Edge(int u,int v,int d) {
e[cnt].dis = d; // distance
e[cnt].to = v;// End
e[cnt].next = head[u];
head[u] = cnt++;
}
struct Node{
int dis,pos;// dis For the shortest distance ,pos Is number .
bool operator <(const Node &x) const {
return x.dis < dis; // Heap , x.dis > dis Big root pile
}
};
priority_queue <Node> Q;
void Dijkstra(int s) {
dis[s] = 0;
Q.push((Node){0,s});
while(!Q.empty()) {
Node temp = Q.top();
Q.pop();
int x = temp.pos,d = temp.dis;
if(vis[x]) continue;
vis[x] = 1;
for(int i = head[x]; ~i; i = e[i].next) {
int y = e[i].to;
if(!vis[y] && dis[y] > dis[x] + e[i].dis) {
dis[y] = dis[x] + e[i].dis;
Q.push((Node){dis[y],y});
}
}
}
while(!Q.empty()) Q.pop();
}
int main() {
int n,m,s,a,b,v,t;
while(~scanf("%d%d%d",&n,&m,&s)) {
for(int i = 0; i <= n; i++) {
dis[i] = MAX;
vis[i] = 0;
head[i] = -1;
}
cnt = 0;
for(int i = 1; i <= m; i++) {
scanf("%d%d%d",&a,&b,&v);
add_Edge(b,a,v);// Reverse construction .
}
Dijkstra(s);
scanf("%d",&t);
int ans = MAX;
for(int i = 1; i <= t; i++) {
scanf("%d",&a);
ans = min(ans,dis[a]);
}
if(ans == MAX) printf(N"-1\n");
else printf("%d\n",ans);
}
return 0;
}
版权声明
本文为[@Li Sicheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221311017744.html
边栏推荐
- How to use colormaps and customize your favorite colorbar?
- MapReduce案例—分别通过Reduce端和Map端实现JOIN操作
- 如何实现数组和 List 之间的转换?
- Rsync remote synchronization
- VMware虚拟机克隆后NAT模式下网络的配置
- The R language uses the qnorm function to generate the positive distribution quantile function data, and uses the plot function to visualize the positive distribution quantile function data (normal di
- RT-Thread配置SPI-Flash(W25Q256)
- Rust实现斐波那契数
- Oplg: new generation cloud primary observable best practices
- From construction to governance, the industry's first white paper on microservice governance technology was officially released (including a free download link)
猜你喜欢
随机推荐
奈飞大跌3500亿,爱优腾能靠涨价走出困境吗?
Tobin Q data - Shanghai and Shenzhen A-share listed companies (including industry name, code and other indicators) 2003-2020
SQL database operation of C (source code)
分省创新能力面板数据 - 含专利数、成交额等多指标数据(2008-2019年)
分享下最近遇到的5种网站变慢的案例
Mpu6050-dmp cannot read data
最大匹配数,最小路径覆盖数,最大独立数,最小点覆盖数 定理总结
各省GTFP绿色全要素生产率面板数据(2004-2018年)
Chrome多设备书签同步方案
The design method and type of flexible printed circuit board (PCB) are analyzed in detail
上市公司营业收入数据集(1990-2021第三季度)
如何成为开源数据库开发人员?
Model based RL概述
分块——优雅的暴力
抖音快手卧榻之侧,黄光裕难以酣睡
Scratch编程入门
Type requirements for parameters pT1 and pT2 of OpenCV function line()
mysql FUNCTION xxx.charindex does not exist
Graph search of obstacles in far planner
The decision curve analysis (DCA) function is written in R language, and multiple decision curves are analyzed. DCA curves are visualized in the same image
![[introduction to keras] MNIST dataset classification](/img/8f/1f9f8674c3212223af85eaec3a34f0.png)








