当前位置:网站首页>洛谷 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
边栏推荐
- Introduction to 3 d games beginners essential 】 【 modeling knowledge
- 【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
- [Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation
- flask的配置文件
- Colocate Join :ClickHouse的一种高性能分布式join查询模型
- Linux服务器安装Redis,详细步骤。
- Modern Privacy-Preserving Record Linkage Techniques: An Overview论文总结
- 力扣150-逆波兰表达式求值——栈实现
- 电脑如何去掉u盘写保护的状态
- 365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
猜你喜欢

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

whois information collection & corporate filing information

3D游戏建模学习路线

Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】

【初学必备】3d游戏建模入门基础知识

电脑如何去掉u盘写保护的状态
[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

【深度学习前沿应用】图像风格迁移

主动信息收集

Redis 持久化机制
随机推荐
cordova 安装错误 Command failed: powershell 解决方法
魔方电池如何“躺赢”?解锁荣威iMAX8 EV“头等舱”安全密码
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
[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
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
“2022零信任神兽方阵”启动调研,欢迎各单位填报信息
「POJ 3666」Making the Grade 题解(两种做法)
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
When selecting a data destination when creating an offline synchronization node - an error is reported in the table, the database type is adb pg, what should I do?
Optimizing Bloom Filter: Challenges, Solutions, and Comparisons论文总结
YOLOv3 SPP source analysis
flask装饰器版登录、session
365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
mysql----group by、where以及聚合函数需要注意事项
Tf铁蛋白颗粒包载顺铂/奥沙利铂/阿霉素/甲氨蝶呤MTX/紫杉醇PTX等药物
TDD、FDD是什么意思?
LeetCode·283.移除零·双指针
QoS服务质量七交换机拥塞管理
QoS Quality of Service Seven Switch Congestion Management
转铁蛋白修饰长春新碱-粉防己碱脂质体|转铁蛋白修饰共载紫杉醇和金雀异黄素脂质体(试剂)