代码随想录算法训练营第六十天 | 图论part10

94. 城市间货物运输 I

对于Bellman_ford算法的优化,松弛n-1次,并且每一次都松弛每一条边,其实做了许多没有意义的事情。实际上只去松弛上一次计算过的节点作为出发节点的边即可。

cpp 复制代码
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <fstream>
#include <climits>

using namespace std;

struct Edge {
	int end, val;
	Edge(int end, int val) :end(end), val(val) {}
};

int main() {
	int n, m;
	int s, e, v;
	ifstream infile("input.txt");
	cin >> n >> m;
	vector<list<Edge>> graph(n + 1);
	
	while (m--) {
		cin >> s >> e >> v;
		graph[s].emplace_back(Edge(e, v));
	}
	
	// 记录到源点的距离的一维数组  
	vector<int> minDist(n + 1, INT_MAX);
	// 记录上一次计算的节点
	queue<int> que;

	minDist[1] = 0;
	que.push(1);
	
	while (!que.empty()) {
		int cur = que.front();
		que.pop();

		for (Edge e : graph[cur]) {
			if (minDist[e.end] > minDist[cur] + e.val) {
				minDist[e.end] = minDist[cur] + e.val;
				que.push(e.end);
			}
		}
		
	}
	
	if (minDist[n] == INT_MAX) cout << "unconnected" << endl;
	else cout << minDist[n] << endl;
	return 0;
}

95. 城市间货物运输 II

只需要记录每个节点入队列的次数,超过n-1直接输出circle然后返回。

cpp 复制代码
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <fstream>
#include <climits>

using namespace std;

struct Edge {
	int end, val;
	Edge(int end, int val) :end(end), val(val) {}
};

int main() {
	int n, m;
	int s, e, v;
	ifstream infile("input.txt");
	cin >> n >> m;
	vector<list<Edge>> graph(n + 1);
	
	while (m--) {
		cin >> s >> e >> v;
		graph[s].emplace_back(Edge(e, v));
	}
	
	// 记录到源点的距离的一维数组  
	vector<int> minDist(n + 1, INT_MAX);
	// 记录上一次计算的节点
	queue<int> que;
	// 记录每一个节点入队列的次数,不超过n-1
	vector<int> count(n + 1, 0);

	minDist[1] = 0;
	que.push(1);
	count[1]++;
	
	while (!que.empty()) {
		int cur = que.front();
		que.pop();

		for (Edge e : graph[cur]) {
			if (minDist[e.end] > minDist[cur] + e.val) {
				minDist[e.end] = minDist[cur] + e.val;
				que.push(e.end);
				count[e.end]++;
				if (count[e.end] > n - 1) {
					cout << "circle" << endl;
					return 0;
				}
			}
		}
		
	}
	
	if (minDist[n] == INT_MAX) cout << "unconnected" << endl;
	else cout << minDist[n] << endl;
	return 0;
}

96. 城市间货物运输 III

对所有边松弛一次,相当于计算 起点到达与起点同一条边的节点的最短距离。

所以应该最多松弛k+1条边。并且每次松弛只能使用上次结果的副本。

cpp 复制代码
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <fstream>
#include <climits>

using namespace std;

struct Edge {
	int source, end, val;
	Edge(int source, int end, int val) :source(source), end(end), val(val) {}
};

int main() {
	int n, m;
	int s, e, v;
	int src, dst, k;
	ifstream infile("input.txt");
	cin >> n >> m;
	vector<Edge> graph;
	
	while (m--) {
		cin >> s >> e >> v;
		graph.emplace_back(Edge{s, e, v});
	}
	cin >> src >> dst >> k;

	// 记录到源点的距离的一维数组  
	vector<int> minDist(n + 1, INT_MAX);
	// 上一轮的副本
	vector<int> minDistCopy(n + 1);

	minDist[src] = 0;
	
	for (int i = 0; i <= k; ++i) { // 重复k+1次
		minDistCopy = minDist;

		for (Edge edge : graph) {
			if (minDistCopy[edge.source] != INT_MAX && minDist[edge.end] > minDistCopy[edge.source] + edge.val) {
				minDist[edge.end] = minDistCopy[edge.source] + edge.val;
			}
		}
	}
	
	
	if (minDist[dst] == INT_MAX) cout << "unreachable" << endl;
	else cout << minDist[dst] << endl;
	return 0;
}
相关推荐
源代码•宸2 分钟前
Leetcode—509. 斐波那契数【简单】
经验分享·算法·leetcode·面试·golang·记忆化搜索·动规
博大世界1 小时前
matlab结构体数组定义
数据结构·算法
Loo国昌1 小时前
【LangChain1.0】第九阶段:文档处理工程 (LlamaIndex)
人工智能·后端·python·算法·langchain
Zach_yuan1 小时前
面向对象封装线程:用 C++ 封装 pthread
开发语言·c++·算法
安特尼2 小时前
X 推荐算法分析
算法·机器学习·推荐算法
罗湖老棍子3 小时前
强迫症冒险家的任务清单:字典序最小拓扑排序
数据结构·算法·图论·拓扑排序
不穿格子的程序员4 小时前
从零开始写算法——回溯篇4:分割回文串 + N皇后
算法·深度优先·dfs
ScilogyHunter4 小时前
qBI有什么用
算法·qbi
龙山云仓4 小时前
No131:AI中国故事-对话荀子——性恶论与AI约束:礼法并用、化性起伪与算法治理
大数据·人工智能·深度学习·算法·机器学习
夏鹏今天学习了吗4 小时前
【LeetCode热题100(90/100)】编辑距离
算法·leetcode·职场和发展