当前位置:网站首页>洛谷 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
边栏推荐
- 【greenDao】Cannot access ‘org.greenrobot.greendao.AbstractDaoSession‘ which is a supertype of
- Metasploit——渗透攻击模块(Exploit)
- [教你做小游戏] 只用几行原生JS,写一个函数,播放音效、播放BGM、切换BGM
- 铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
- QoS Quality of Service Eight Congestion Avoidance
- 多线程与高并发(五)—— 源码解析 ReentrantLock
- 端口探测详解
- whois信息收集&企业备案信息
- 电脑如何去掉u盘写保护的状态
- 电脑开不了机是什么原因?
猜你喜欢
Site Architecture Detection & Chrome Plugin for Information Gathering
【C#】WCF和TCP消息通信练习,实现群聊功能
YOLOv3 SPP source analysis
转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
uni-app 数据上拉加载更多功能
Linux服务器安装Redis,详细步骤。
We used 48h to co-create a web game: Dice Crush, to participate in international competitions
[Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation
铁蛋白-AHLL纳米颗粒|人表皮生长因子-铁蛋白重链亚基纳米粒子(EGF-5Cys-FTH1)|铁蛋白颗粒包载氯霉素Chloramphenicol-Ferritin
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
随机推荐
转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
QoS服务质量八拥塞避免
QoS Quality of Service Seven Switch Congestion Management
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
YOLOv3 SPP source analysis
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
@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
皮质-皮质网络的多尺度交流
What is the upstream bandwidth and downstream bandwidth of the server?
多功能纳米酶Ag/PANI|柔性衬底纳米ZnO酶|铑片纳米酶|Ag-Rh合金纳米颗粒纳米酶|铱钌合金/氧化铱仿生纳米酶
优化是一种习惯●出发点是'站在靠近临界'的地方
“2022零信任神兽方阵”启动调研,欢迎各单位填报信息
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec Paper Summary
Common ports and services
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
The servlet mapping path matching resolution
QoS服务质量七交换机拥塞管理
【初学必备】3d游戏建模入门基础知识
Pt/CeO2单原子纳米酶|[email protected] NPs纳米酶|碳纳米管负载铂颗粒纳米酶|白血病拮抗多肽修饰的FeOPtPEG复合纳米酶