最短路(Bellman Ford) – 算法板子

#include<bits/stdc++.h> using namespace std; int n, m, k; const int N = 510, M = 1e4 + 10; int d[N], backup[N]; struct Edge{     int a, b, c; } edges[M];  int bellman_ford() {     memset(d, 0x3f, sizeof d);     d[1] = 0;     for (int i = 1; i <= k; ++i) {         memcpy(backup, d, sizeof d);         for (int j = 1; j <= m; ++j) {             int a = edges[j].a, b = edges[j].b, c = edges[j].c;             d[b] = min(d[b], backup[a] + c);         }     }     if (d[n] > 0x3f3f3f3f / 2) {         return -1;     } else {         return d[n];     } };  int main() {     scanf("%d%d%d", &n, &m, &k);     for (int i = 1; i <= m; ++i) {         int a, b, c;         scanf("%d%d%d", &a, &b, &c);         edges[i] = {a, b, c};     }          int rs = bellman_ford();          if (rs == -1) {         puts("impossible");     } else {         printf("%dn", rs);     }     return 0; }

最短路(Bellman Ford) – 算法板子leetcode刷题题解部分资料来自网络,侵权毕设源码联系删除

区块链毕设网(www.qklbishe.com)全网最靠谱的原创区块链毕设代做网站
部分资料来自网络,侵权联系删除!
资源收费仅为搬运整理打赏费用,用户自愿支付 !
qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台 » 最短路(Bellman Ford) – 算法板子leetcode刷题题解