当前位置:网站首页>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;
}
边栏推荐
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- ICDE 2023|TKDE Poster Session(CFP)
- C file input operation
- 搭建主从模式集群redis
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- Mobile robot path planning: artificial potential field method [easy to understand]
- 安装单机redis详细教程
- C文件输入操作
- 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
- 使用xml文件打印mybaties-log插件的方式
猜你喜欢
Yolov3 trains its own data set to generate train txt
Juypter notebook modify the default open folder and default browser
冒泡排序数组
codeforces每日5题(均1700)-第四天
Thread application instance
zabbix5客户端安装和配置
Codeworks 5 questions per day (1700 average) - day 4
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Novice must see, click two buttons to switch to different content
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
随机推荐
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
高级性能测试系列《24. 通过jdbc执行sql脚本》
电脑使用哪个录制视频软件比较好
Golang并发编程——goroutine、channel、sync
二进制操作
性能测试如何创造业务价值
Watchful pioneer world outlook Architecture - (how does a good game come from)
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
codeforces每日5题(均1700)-第四天
新手必看,點擊兩個按鈕切換至不同的內容
思维意识转变是施工企业数字化转型成败的关键
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Date tool class (updated from time to time)
Markdown基础语法
2022 software engineering final exam recall Edition
Page title component
Fastdfs installation
Memory management of C
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5