当前位置:网站首页>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 
边栏推荐
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
- arcgis js 4. Add pictures to x map
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- Is the neural network (pinn) with embedded physical knowledge a pit?
- 模块化 CommonJS ES Module
- Writing method of then part in drools
- Day12 control flow if switch while do While guessing numbers game
- [old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
- . Net, C # basic knowledge
- 深拷貝 事件總線
猜你喜欢

高性能纠删码编码

js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async

kubenetes中port、targetPort、nodePort、containerPort的区别与联系

Deep copy event bus

线性DP AcWing 898. 数字三角形

线性DP AcWing 902. 最短编辑距离

Find the common ancestor of any two numbers in a binary tree

Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)

区间DP AcWing 282. 石子合并

AI mid stage technology research
随机推荐
Redis bloom filter
BOM DOM
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
C#修饰符
H5 to app
BOM DOM
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
About asp Net MVC project in local vs running response time is too long to access, the solution!
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
Shuttle encapsulated AppBar
BOM DOM
Shutter encapsulated button
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
高性能纠删码编码
通过反射执行任意类的任意方法
软件测试面试题-2022年大厂面试题合集
Sweetheart leader: Wang Xinling
Anti shake throttle
Docker compose configuration mysql, redis, mongodb
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area