当前位置:网站首页>bellman-ford AcWing 853. 有边数限制的最短路
bellman-ford AcWing 853. 有边数限制的最短路
2022-07-02 09:43:00 【T_Y_F666】
bellman-ford AcWing 853. 有边数限制的最短路
原题链接
算法标签
最短路 Bellman-Ford
思路
由题意 存在负边, 因此不能采用Dijkstra
由于具有边数限制
故采用Bellman-Ford
Bellman-Ford算法
对于存在负权回路的路径
可能不存在最短路
例如下图
1至5最短路 不存在 由于每次在2, 3, 4回路中循环一次, 路几个长度减1
代码
#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存储节点祖宗节点 d存储当前节点据祖宗节点的距离
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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- LeetCode—剑指 Offer 51. 数组中的逆序对
- Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
- 中国交通标志检测数据集
- 防抖 节流
- The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
- post请求体内容无法重复获取
- 2.7 binary tree, post order traversal - [FBI tree]
- MySQL与PostgreSQL抓取慢sql的方法
- 甜心教主:王心凌
- Intel internal instructions - AVX and avx2 learning notes
猜你喜欢
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Writing method of then part in drools
Drools dynamically add, modify, and delete rules
Find the common ancestor of any two numbers in a binary tree
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Sparkcontext: error initializing sparkcontext solution
"As a junior college student, I found out how difficult it is to counter attack after graduation."
线性DP AcWing 902. 最短编辑距离
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
分布式机器学习框架与高维实时推荐系统
随机推荐
WSL 2 will not be installed yet? It's enough to read this article
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Multiply LCA (nearest common ancestor)
Writing method of then part in drools
Drools executes string rules or executes a rule file
[C language] convert decimal numbers to binary numbers
mysql数据库基础
SparkContext: Error initializing SparkContext解决方法
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Openssh remote enumeration username vulnerability (cve-2018-15473)
FBX import under ue4/ue5 runtime
Addition, deletion, modification and query of MySQL table (Advanced)
怎样写一篇赏心悦目的英文数学论文
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
mysql索引和事务
[FFH] little bear driver calling process (take calling LED light driver as an example)
Performance tuning project case
Differences between nodes and sharding in ES cluster
String palindrome hash template question o (1) judge whether the string is palindrome
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60