当前位置:网站首页>AcWing 1137. Select the best line solution (the shortest circuit)
AcWing 1137. Select the best line solution (the shortest circuit)
2022-07-02 19:32:00 【Mr. Qiao Da】
AcWing 1137. Select the best route
Their thinking : Each starting point will run once, and the shortest circuit will timeout , Then establish a virtual starting point , The distance between this initial point and all starting points is 0, So the shortest path from the virtual starting point to the end point is the shortest and shortest path among all the starting points
#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;
}
边栏推荐
猜你喜欢

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

Thread application instance

Use cheat engine to modify money, life and stars in Kingdom rush

线程应用实例

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

Markdown基础语法

高级性能测试系列《24. 通过jdbc执行sql脚本》

Educational Codeforces Round 129 (Rated for Div. 2) 补题题解

What is 9D movie like? (+ common sense of dimension space)

《重构:改善既有代码的设计》读书笔记(下)
随机推荐
450-深信服面经1
Qpropertyanimation use and toast case list in QT
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
2022 compilation principle final examination recall Edition
In pytorch function__ call__ And forward functions
Golang:[]byte to string
Compile oglpg-9th-edition source code with clion
Mobile robot path planning: artificial potential field method [easy to understand]
Juypter notebook modify the default open folder and default browser
第七章-类基础
《重构:改善既有代码的设计》读书笔记(下)
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
MySQL高级(进阶)SQL语句
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Thread application instance
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
PHP非对称加密方法私钥及公钥加密解密的方法