当前位置:网站首页>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 factor analysis
- Gmapping code analysis [easy to understand]
- 2022.7.1-----leetcode. two hundred and forty-one
- 《架构整洁之道》读书笔记(下)
- Emmet基础语法
- Use cheat engine to modify money, life and stars in Kingdom rush
- Virtual machine initialization script, virtual machine mutual secret key free
- Thread application instance
- mysql备份后缀是什么_mysql备份还原
- 移动机器人路径规划:人工势场法[通俗易懂]
猜你喜欢

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

Develop fixed asset management system, what voice is used to develop fixed asset management system

Markdown基础语法

Markdown basic grammar

思维意识转变是施工企业数字化转型成败的关键

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

【测试开发】软件测试—概念篇

Refactoring: improving the design of existing code (Part 1)

搭建哨兵模式reids、redis从节点脱离哨兵集群
随机推荐
SIFT feature point extraction "suggestions collection"
高级性能测试系列《24. 通过jdbc执行sql脚本》
PHP parser badminton reservation applet development requires online system
Use cheat engine to modify money, life and stars in Kingdom rush
ORA-01455: converting column overflows integer datatype
《代碼整潔之道》讀書筆記
High frequency interview questions
第七章-类基础
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Emmet基础语法
451-memcpy、memmove、memset的实现
Preprocessing and preprocessing macros
仿京东放大镜效果(pink老师版)
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
PHP asymmetric encryption method private key and public key encryption and decryption method
数字滚动带动画
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
新手必看,点击两个按钮切换至不同的内容
使用xml文件打印mybaties-log插件的方式
Notes de lecture sur le code propre