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

代码随想录算法训练营第五十九天 | 图论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;// 更新非访问节点的minDistfor (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.mrgr.cn/news/16370.html

相关文章:

  • nacos获取服务实例流程
  • MP条件构造器之常用功能详解(or、and、exists、notExists)
  • Python 高级特效 - 生成器 ( Generator)
  • DAY59-图论-Bellman_ford
  • HCIP笔记12-交换(1)
  • cnocr 安装
  • Web开发 Ajax 2024/3/31
  • 【C++题解】1722 - 输出两位的巧数
  • 内存管理篇-16二级页表工作原理
  • 揭秘!糖尿病:从绝望到希望的治愈之路
  • Java高级Day34-流补充
  • 【自由能系列(初级)】第一性原理与自由能——从基础到系统做功的桥梁
  • 52基于SpringBoot+Vue+uniapp的旅游管理系统的的详细设计和实现(源码+lw+部署文档+讲解等)
  • 【STM32】BKP备份寄存器与RTC实时时钟
  • Stable Diffusion 必备插件推荐,菜鸟轻松成高手!
  • 海外融合CDN怎样优化?
  • C#学习笔记(二)安装开发环境、代码编译运行
  • Windows系统下不小心把输入法切换成了繁体怎么办
  • <数据集>车辆识别数据集<目标检测>
  • win10环境下gvim离线配置插件的一些补充