代码随想录算法训练营第五十九天 | 图论part09

47. 参加科学大会

使用邻接表和堆来优化dijkstra算法。原来的时间复杂度是 O ( n 2 ) O(n^2) O(n2),n是节点数量。

使用堆优化,从宏观角度来说就是将每条边都加入堆,一共是E条边,每次操作的时间复杂度是 l o g ( E ) log(E) log(E),所以时间复杂度是 E l o g ( E ) Elog(E) Elog(E)。

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


using namespace std;

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

class myCompare {
public:
	bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
		return a.second > b.second;
	}
};


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<bool> visited(n + 1, false);
	// 记录到源点的距离的一维数组  
	vector<int> minDist(n + 1, INT_MAX);
	// 记录目前已知到源点距离最短的点和距离
	priority_queue <pair<int, int>, vector<pair<int, int>>, myCompare> pq;

	minDist[1] = 0;
	pq.push({ 1, 0 });

	while (!pq.empty()) {
		// 找到距离源点最近的节点
		pair<int, int> cur = pq.top(); pq.pop();
		if (visited[cur.first]) continue;

		// 将节点标记为已访问
		visited[cur.first] = true;

		// 更新非访问节点的minDist
		for (Edge edge : graph[cur.first]) {
			if (!visited[edge.end] && edge.val + cur.second < minDist[edge.end]) {
				minDist[edge.end] = edge.val + cur.second;
				pq.push({ edge.end, minDist[edge.end] });
			}
		}
		
	}
	if (minDist[n] == INT_MAX) cout << -1 << endl;
	else cout << minDist[n] << endl;
	return 0;
}

94. 城市间货物运输 I

Bellman_ford算法就是松弛n-1次。每一次会将所有点到源点的距离更新。

使用Bellman_ford算法,需要以下数据结构:

  • 邻接表
  • 记录到源点的距离的一维数组
cpp 复制代码
#include <iostream>
#include <vector>
#include <list>
#include <fstream>
#include <climits>

using namespace std;

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


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

	minDist[1] = 0;
	
	for (int i = 1; i < n; ++i) { // 重复n-1次

		for (Edge& e : graph) {
			if (minDist[e.from] != INT_MAX && minDist[e.end] > minDist[e.from] + e.val) {
				minDist[e.end] = minDist[e.from] + e.val;
			}
		}
		
	}
	
	if (minDist[n] == INT_MAX) cout << "unconnected" << endl;
	else cout << minDist[n] << endl;
	return 0;
}
相关推荐
未来之窗软件服务10 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
豐儀麟阁贵11 小时前
基本数据类型
java·算法
乐迪信息12 小时前
乐迪信息:基于AI算法的煤矿作业人员安全规范智能监测与预警系统
大数据·人工智能·算法·安全·视觉检测·推荐算法
hsjkdhs13 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
立志成为大牛的小牛13 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
星空下的曙光13 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
StarPrayers.15 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
爱吃橘的橘猫15 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
2351615 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
weixin_3077791316 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json