当前位置:网站首页>洛谷 P1629 邮递员送信 (三种最短路)
洛谷 P1629 邮递员送信 (三种最短路)
2022-08-10 19:07:00 【eyuhaobanga】
题意容易理解,从1出发到其他所有点并返回的最短路,需要先正着跑一边,然后建反边再跑一边
Dijkstra:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> G(n + 1), G1(n + 1); vector<int> dist(n + 1, 0x3f3f3f3f), dist1(n + 1, 0x3f3f3f3f); rep (i, 0, m) { int u, v, c; cin >> u >> v >> c; G[u].push_back(make_pair(v, c)); G1[v].push_back(make_pair(u, c)); } LL ans = 0; dist[1] = 0; set<pair<int, int>> se, se1; for (int i = 1; i <= n; i++) { se.insert(make_pair(dist[i], i)); } while (!se.empty()) { int u = se.begin()->second; se.erase(se.begin()); if (dist[u] > 1 << 30) { break; } for (auto v : G[u]) { if (dist[u] + v.second < dist[v.first]) { se.erase(make_pair(dist[v.first], v.first)); dist[v.first] = dist[u] + v.second; se.insert(make_pair(dist[v.first], v.first)); } } } for (int i = 1; i <= n; i++) { ans += dist[i]; } dist1[1] = 0; for (int i = 1; i <= n; i++) { se1.insert(make_pair(dist1[i], i)); } while (!se1.empty()) { int u = se1.begin()->second; se1.erase(se1.begin()); if (dist1[u] > 1 << 30) { break; } for (auto v : G1[u]) { if (dist1[u] + v.second < dist1[v.first]) { se1.erase(make_pair(dist1[v.first], v.first)); dist1[v.first] = v.second + dist1[u]; se1.insert(make_pair(dist1[v.first], v.first)); } } } for (int i = 1; i <= n; i++) { ans += dist1[i]; } cout << ans << '\n'; return 0; }bellman ford:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; struct Edge { int x, y, v; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<Edge> G(m + 1), G1(m + 1); for (int i = 1; i <= m; i++) { int u, v, c; cin >> u >> v >> c; G[i].x = u; G[i].y = v; G[i].v = c; G1[i].x = v; G1[i].y = u; G1[i].v = c; } vector<int> dist(n + 1, 0x3f3f3f3f); dist[1] = 0; while (true) { bool ok = false; for (int i = 1; i <= m; i++) { int x = G[i].x, y = G[i].y, w = G[i].v; if (dist[x] < 1 << 30) { if (dist[x] + w < dist[y]) { dist[y] = dist[x] + w; ok = true; } } } if (!ok) { break; } } vector<int> dist1(n + 1, 0x3f3f3f3f); dist1[1] = 0; while (true) { bool ok = false; for (int i = 1; i <= m; i++) { int x = G1[i].x, y = G1[i].y, w = G1[i].v; if (dist1[x] < 1 << 30) { if (dist1[x] + w < dist1[y]) { dist1[y] = w + dist1[x]; ok = true; } } } if (!ok) { break; } } LL ans = 0; for (int i = 2; i <= n; i++) { ans += dist1[i] + dist[i]; } cout << ans << '\n'; return 0; }floyd (n>=1000慎用,该题吸氧可过):
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> a(n + 1, vector<int> (n + 1, 0x3f3f3f3f)); rep (i, 0, m) { int u, v, c; cin >> u >> v >> c; a[u][v] = min(a[u][v], c); } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a[i][j] = min(a[i][j], a[i][k] + a[k][j]); } } } LL ans = 0; for (int i = 2; i <= n; i++) { ans = ans + a[1][i] + a[i][1]; } cout << ans << '\n'; return 0; }关于spfa,它已经死了,请不要在任何公共场合写spfa
边栏推荐
- MATLAB设计,FPGA实现,联合ISE和Modelsim仿真的FIR滤波器设计
- flask的配置文件
- 关于npm/cnpm/npx/pnpm与yarn
- 【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
- 一维数组动态和问题答记
- Redis 持久化机制
- 我们用48h,合作创造了一款Web游戏:Dice Crush,参加国际赛事
- 服务器上行带宽和下行带宽指的是什么
- 【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
- [Go WebSocket] Your first Go WebSocket server: echo server
猜你喜欢

Optimizing Bloom Filter: Challenges, Solutions, and Comparisons论文总结

子域名收集&Google搜索引擎语法
[email protected] NPs)"/>转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)

【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习

FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec Paper Summary

巧用RoaringBitMap处理海量数据内存diff问题

基于TCP的聊天系统

QoS服务质量六路由器拥塞管理

史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)

QoS Quality of Service Seven Switch Congestion Management
随机推荐
【毕业设计】基于Stm32的智能疫情防控门禁系统 - 单片机 嵌入式 物联网
转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体
DefaultSelectStrategy NIOEventLoop执行策略
[Teach you how to make a small game] Write a function with only a few lines of native JS to play sound effects, play BGM, and switch BGM
【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
What is the upstream bandwidth and downstream bandwidth of the server?
QoS Quality of Service Eight Congestion Avoidance
Pt/CeO2单原子纳米酶|[email protected] NPs纳米酶|碳纳米管负载铂颗粒纳米酶|白血病拮抗多肽修饰的FeOPtPEG复合纳米酶
keepalived:故障检测自动修复脚本
QoS Quality of Service Six Router Congestion Management
报错:runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
QoS Quality of Service Seven Switch Congestion Management
30分钟使用百度EasyDL实现健康码/行程码智能识别
365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
力扣18-四数之和——双指针法
2022 Hangdian Multi-School Seven Black Magic (Sign-in)
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
TDD、FDD是什么意思?
LeetCode·283.移除零·双指针
电脑为什么会蓝屏的原因