当前位置:网站首页>bellman-ford AcWing 853. Shortest path with side limit
bellman-ford AcWing 853. Shortest path with side limit
2022-07-02 12:43:00 【T_ Y_ F666】
bellman-ford AcWing 853. The shortest path with limited number of edges
Original link
AcWing 853. The shortest path with limited number of edges
Algorithm tags
shortest path Bellman-Ford
Ideas
From the meaning of the title There are negative edges , So we can't use Dijkstra
Due to the limitation of the number of sides
Therefore adopt Bellman-Ford
Bellman-Ford Algorithm
For paths with negative weight loops
There may be no shortest circuit
Such as below
1 to 5 shortest path non-existent Because every time in 2, 3, 4 Loop once , How many lengths of the road minus 1
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 10005;
// p Storage node ancestor node d Store the distance between the current node and the ancestor node
int lst[N], dis[N];
int n,m,k;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
struct Edge{
int a, b, c;
}edge[N];
int be(){
memset(dis, 0x3f3f, sizeof dis);
dis[1]=0;
rep(i, 0, k){
memcpy(lst, dis, sizeof dis);
rep(j, 0, m){
Edge e=edge[j];
dis[e.b]=min(dis[e.b], lst[e.a]+e.c);
}
}
if(dis[n]>0x3f3f3f3f/2){
return -1;
}else{
return dis[n];
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read(), m=read(), k=read();
rep(i, 0, m){
int x=read(), y=read(), z=read();
edge[i]={x, y, z};
}
if(be()==-1){
puts("impossible");
}else{
printf("%lld", dis[n]);
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- Intel internal instructions - AVX and avx2 learning notes
- Find the common ancestor of any two numbers in a binary tree
- . Net wechat message template push
- Use MySQL events to regularly perform post seven world line tasks
- 线性DP AcWing 899. 编辑距离
- 哈希表 AcWing 840. 模拟散列表
- Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
- 线性DP AcWing 902. 最短编辑距离
- 模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
猜你喜欢
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
2.6 using recursion and stack - [tower of Hanoi problem]
async/await 异步函数
In development, why do you find someone who is paid more than you but doesn't write any code?
Execute any method of any class through reflection
趣味 面试题
bellman-ford AcWing 853. 有边数限制的最短路
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
随机推荐
Enhance network security of kubernetes with cilium
LeetCode—<动态规划专项>剑指 Offer 19、49、60
线性DP AcWing 895. 最长上升子序列
通过反射执行任意类的任意方法
BOM DOM
3 A VTT端接 稳压器 NCP51200MNTXG资料
线性DP AcWing 897. 最长公共子序列
Leetcode - Sword finger offer 59 - I, 59 - II
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Input box assembly of the shutter package
Fluent fluent library encapsulation
[ybtoj advanced training guide] similar string [string] [simulation]
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
C#运算符
Use sqoop to export ads layer data to MySQL
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Bom Dom
Distributed machine learning framework and high-dimensional real-time recommendation system
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
Go learning notes - multithreading