当前位置:网站首页>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;
}
边栏推荐
- SIFT feature point extraction "suggestions collection"
- PHP asymmetric encryption method private key and public key encryption and decryption method
- Advanced performance test series "24. Execute SQL script through JDBC"
- Chapter 7 - class foundation
- AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
- Reading notes of "the way to clean structure" (Part 2)
- 全志A33使用主线U-Boot
- Memory management of C
- Use cheat engine to modify money, life and stars in Kingdom rush
- AcWing 1135. 新年好 题解(最短路+搜索)
猜你喜欢
程序猿入门攻略(十二)——数据的存储
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Usage of ieda refactor
《重构:改善既有代码的设计》读书笔记(下)
Advanced performance test series "24. Execute SQL script through JDBC"
Markdown基础语法
How performance testing creates business value
High frequency interview questions
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
juypter notebook 修改默认打开文件夹以及默认浏览器
随机推荐
Introduction of Ethernet PHY layer chip lan8720a
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
IEDA refactor的用法
以太网PHY层芯片LAN8720A简介
第七章-类基础
Talk about the design of red envelope activities in e-commerce system
Mobile robot path planning: artificial potential field method [easy to understand]
Memory management of C
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
AcWing 1129. 热浪 题解(最短路—spfa)
PHP parser badminton reservation applet development requires online system
Digital scroll strip animation
【pytorch学习笔记】Tensor
程序猿入门攻略(十二)——数据的存储
Horizontal ultra vires and vertical ultra vires [easy to understand]
Thread application instance
PHP-Parser羽毛球预约小程序开发require线上系统
MySQL高级(进阶)SQL语句