当前位置:网站首页>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 
边栏推荐
- About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
- Calculate the maximum path sum of binary tree
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- Redis bloom filter
- 浏览器存储方案
- Find the common ancestor of any two numbers in a binary tree
- Go learning notes - go based interprocess communication
- The differences and relationships among port, targetport, nodeport and containerport in kubenetes
- AI mid stage technology research
- 高性能纠删码编码
猜你喜欢

BOM DOM

Redis sentinel mechanism and configuration

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

线性DP AcWing 899. 编辑距离

深拷贝 事件总线

The differences and relationships among port, targetport, nodeport and containerport in kubenetes

Openssh remote enumeration username vulnerability (cve-2018-15473)

Bom Dom

Floyd AcWing 854. Floyd求最短路

分布式机器学习框架与高维实时推荐系统
随机推荐
Writing method of then part in drools
JSON序列化 与 解析
百款拿来就能用的网页特效,不来看看吗?
哈希表 AcWing 840. 模拟散列表
AI中台技术调研
[ybtoj advanced training guidance] cross the river [BFS]
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
8 examples of using date commands
Docsify deploy IIS
Day12 control flow if switch while do While guessing numbers game
Shuttle encapsulated AppBar
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
Go learning notes - multithreading
Some sudden program ideas (modular processing)
Sweetheart leader: Wang Xinling
H5 to app
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
Drools executes the specified rule
LeetCode—剑指 Offer 51. 数组中的逆序对
Go learning notes - go based interprocess communication