当前位置:网站首页>AcWing 1137. 选择最佳线路 题解(最短路)
AcWing 1137. 选择最佳线路 题解(最短路)
2022-07-02 18:27:00 【乔大先生】
AcWing 1137. 选择最佳线路
解题思路:每个起点都跑一次最短路会超时,那就建立一个虚拟初始点,这个初始点距离所有起点的距离都是0,所以这个虚拟初始点到终点的最短路径就是所有起点中最短的最短路
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 40010, INF = 0x3f3f3f3f;
int n, m, T;
int h[N], e[M], ne[M], w[M], idx;
int d[N];
int q[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 ++ ;
}
int spfa(){
memset(d, 0x3f, sizeof d);
int hh = 0, tt = 1;
q[0] = 0;
d[0] = 0;
st[0] = true;
while(tt != hh){
int t = q[hh ++ ];
if(hh == N) hh = 0;
st[t] = false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if(d[j] > d[t] + w[i]){
d[j] = d[t] + w[i];
if(!st[j]){
q[tt ++ ] = j;
if(tt == N) tt = 0;
st[j] = true;
}
}
}
}
if(d[T] > 1e9) return -1;
else return d[T];
}
int main()
{
while(scanf("%d%d%d", &n, &m, &T) != -1){
memset(h, -1, sizeof h);
idx = 0;
while(m -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int w;
cin>>w;
while(w -- ){
int ver;
scanf("%d", &ver);
add(0, ver, 0);
}
cout<<spfa()<<endl;
}
return 0;
}
边栏推荐
- Data dimensionality reduction principal component analysis
- Novice must see, click two buttons to switch to different content
- 451-memcpy、memmove、memset的实现
- PHP parser badminton reservation applet development requires online system
- 《重构:改善既有代码的设计》读书笔记(下)
- [test development] software testing - concept
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- 新手必看,點擊兩個按鈕切換至不同的內容
- Reading notes of "the way to clean structure" (Part 2)
猜你喜欢
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
高频面试题
Quanzhi A33 uses mainline u-boot
Yunna | why use the fixed asset management system and how to enable it
《重构:改善既有代码的设计》读书笔记(下)
ICDE 2023|TKDE Poster Session(CFP)
《重构:改善既有代码的设计》读书笔记(上)
Markdown基础语法
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
论文导读 | 机器学习在数据库基数估计中的应用
随机推荐
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
GMapping代码解析[通俗易懂]
juypter notebook 修改默认打开文件夹以及默认浏览器
Talk about the design of red envelope activities in e-commerce system
思维意识转变是施工企业数字化转型成败的关键
安装单机redis详细教程
冒泡排序数组
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
PHP-Parser羽毛球预约小程序开发require线上系统
Preprocessing and preprocessing macros
Page title component
4274. Suffix expression - binary expression tree
ORA-01455: converting column overflows integer datatype
QT中的QPropertyAnimation使用和toast案列
Watchful pioneer world outlook Architecture - (how does a good game come from)
2022 compilation principle final examination recall Edition
预处理和预处理宏
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
[test development] takes you to know what software testing is
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5