当前位置: 首页 > news >正文

算法训练营|图论第10天 Bellman_ford:优化算法,判断负权算法,单源有限最短路

题目:Bellman_ford:优化算法

题目链接:

94. 城市间货物运输 I (kamacoder.com)

代码:

#include<bits/stdc++.h>
using namespace std;
struct Edge {int to;int val;Edge(int t, int w) :to(t), val(w) {}
};
int main() {int n, m;cin >> n >> m;int p1, p2, val;vector<list<Edge>>grid(n + 1);for (int i = 0; i < m; i++) {cin >> p1 >> p2 >> val;grid[p1].push_back(Edge(p2, val));}int start = 1;int end = n;queue<int>que;vector<bool>InQueue(n + 1, false);vector<int>minDist(n + 1, INT_MAX);minDist[start] = 0;que.push(start);while (!que.empty()) {int cur = que.front();que.pop();InQueue[cur] = false;for (Edge edge : grid[cur]) {int from = cur;int to = edge.to;int val = edge.val;if (minDist[to] > minDist[from] + val) {minDist[to] = minDist[from] + val;if (InQueue[to] == false) {que.push(to);InQueue[to] = true;}}}}if (minDist[end] == INT_MAX) cout << "unconnected" << endl;else cout << minDist[end] << endl;
}

题目:判断负权算法

题目链接:

95. 城市间货物运输 II (kamacoder.com)

代码:

#include<bits/stdc++.h>
using namespace std;
struct Edge {int to;int val;Edge(int t, int w) :to(t), val(w) {}
};
int main() {int n, m;cin >> n >> m;int p1, p2, val;vector<vector<int>>grid;for (int i = 0; i < m; i++) {cin >> p1 >> p2 >> val;grid.push_back({ p1,p2,val });}int start = 1;int end = n;vector<int>minDist(n + 1, INT_MAX);minDist[start] = 0;bool flag = false;for (int i = 1; i <= n ; i++) {for (vector<int>vec : grid) {int from = vec[0];int to = vec[1];int val = vec[2];if (i < n) {if (minDist[from] != INT_MAX && minDist[from] + val < minDist[to]) {minDist[to] = minDist[from] + val;}}else {if (minDist[from] != INT_MAX && minDist[from] + val < minDist[to]) {flag = true;}}}}if (flag) {cout << "circle" << endl;}else if (minDist[end] == INT_MAX) {cout << "unconnected" << endl;}else {cout << minDist[end] << endl;}
}

题目:单源有限最短路

题目链接:

96. 城市间货物运输 III (kamacoder.com)

代码:

#include<bits/stdc++.h>
using namespace std;
struct Edge {int to;int val;Edge(int t, int w) :to(t), val(w) {}
};
int main() {int n, m;cin >> n >> m;int p1, p2, val;vector<vector<int>>grid;for (int i = 0; i < m; i++) {cin >> p1 >> p2 >> val;grid.push_back({ p1,p2,val });}int src, dst, k;cin >> src >> dst >> k;vector<int> minDist(n + 1, INT_MAX);minDist[src] = 0;vector<int> minDist_copy(n + 1); // 用来记录上一次遍历的结果for (int i = 1; i <= k + 1; i++) {minDist_copy = minDist;for (vector<int> &vec : grid) {int from = vec[0];int to = vec[1];int val = vec[2];if (minDist_copy[from] != INT_MAX && minDist[to] > minDist_copy[from] + val) {minDist[to] = minDist_copy[from] + val;}}}if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点else cout << minDist[dst] << endl; // 到达终点最短路径
}


http://www.mrgr.cn/news/18279.html

相关文章:

  • 【框架】在Spring Cloud分布式微服务框架中,一个请求的流转和处理的步骤
  • 干货含源码!如何用Java后端操作Docker(命令行篇)
  • 获取 包 的类名信息以及 使用类名信息反向实例化类
  • 网络是怎样连接的
  • LinkedList与链表
  • nginx配置代理https端口的要点
  • Java项目:128 基于Spring Boot的装饰工程管理系统
  • 【小呆的热力学笔记】典型热机-燃气轮机的理想热力循环
  • Java基础——十一、Lambda
  • 【C++】C++ 多态的底层实现
  • 26. 在集合中删除元素时,为什么使用Iterator.remove()而不是Collection.remove()?
  • 第二十章 rust多平台编译
  • 两个月冲刺软考——概念+求已知内存按字节编址从(A)…到(B)…的存储容量+求采用单/双缓冲区需要花费的时间计算 类型题目讲解
  • 投保单号和保单号码
  • 【Rust】005-Rust 结构体
  • c++修炼之路之C++11
  • 数据链路层(MAC地址)
  • 线程间同步的方式有哪些?
  • 【C++】将myString类中能够实现的操作都实现一遍
  • ARM————体系结构