当前位置:网站首页>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
边栏推荐
- Leetcode - Sword finger offer 37, 38
- Sse/avx instruction set and API of SIMD
- 8 examples of using date commands
- 浏览器node事件循环
- Is the neural network (pinn) with embedded physical knowledge a pit?
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- Docker compose configuration mysql, redis, mongodb
- Distributed machine learning framework and high-dimensional real-time recommendation system
- js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
- 哈希表 AcWing 840. 模拟散列表
猜你喜欢
Anti shake throttle
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
Redis sentinel mechanism and configuration
2.6 using recursion and stack - [tower of Hanoi problem]
Use sqoop to export ads layer data to MySQL
spfa AcWing 851. spfa求最短路
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
随机推荐
堆 AcWing 839. 模拟堆
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
[FFH] little bear driver calling process (take calling LED light driver as an example)
spfa AcWing 851. spfa求最短路
MySQL and PostgreSQL methods to grab slow SQL
8 examples of using date commands
Enhance network security of kubernetes with cilium
LeetCode—剑指 Offer 51. 数组中的逆序对
BOM DOM
C#修饰符
Mui WebView down refresh pull-up load implementation
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
浏览器存储方案
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Mongodb redis differences
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
LeetCode—<动态规划专项>剑指 Offer 19、49、60
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))