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

代码随想录算法训练营第五十九天 | 图论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)

#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算法,需要以下数据结构:

  • 邻接表
  • 记录到源点的距离的一维数组
#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;
}

http://www.kler.cn/news/292572.html

相关文章:

  • 2024数学建模国赛选题建议+团队助攻资料
  • 优化理论及应用精解【4】
  • GNU/Linux - 进程关联的控制终端
  • centos7.9搭建mysql5.6
  • 无论是速卖通、敦煌网、国际站,自养号测评就是提高曝光的利器!
  • 支付宝直付通与微信收付通分账产品:功能差异与适用场景
  • 感知机模型
  • springboot小儿推拿培训系统
  • 蓝牙--关于bta_av_api.cc 文件的讲解
  • java使用jedis连接redis
  • Mac剪贴板管理器:扩展你的复制粘贴能力
  • 卷积公式的几何学理解
  • 硬件工程师笔试面试——继电器
  • windows清理图标缓存
  • 如何实现对窗口window的viewtree进行dump Hierarchy-安卓framework实战开发
  • HarmonyOS开发实战( Beta5版)状态管理优秀实践
  • 二、搭建网站服务器超详细步骤——部署轻量应用服务器(Centos)
  • ceph中pg与pool关系
  • SQL常见100面试题解析
  • vs2019编译opencv+contribute+gpu
  • 【华为OD】2024D卷——查找众数与中位数
  • MacBook真的不能打游戏吗?Mac打游戏会损坏电脑吗?苹果电脑怎么玩游戏
  • 如何在 Java 中实现线程安全的单例模式?
  • 前端宝典二十七:React Native最佳实践实例推荐
  • 强化网络安全:通过802.1X协议保障远程接入设备安全认证
  • 迭代器 Iterator 是什么?
  • Linux修改docker默认存储目录(/var/lib)
  • Twitter上品牌安全指标的关键显示错误已修正
  • 2024跨境旺季营销:哪个平台是流量之王?
  • Ribbon负载均衡底层原理