mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
P1342 请柬
R53781989
This commit is contained in:
parent
2fdd39286a
commit
2f0f88a28f
63
Luogu/problem/P1342/P1342.cpp
Normal file
63
Luogu/problem/P1342/P1342.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, m, u, v;
|
||||
long long w, dist1[100005], dist2[100005], ans;
|
||||
vector<pair<int, long long>> g1[100005], g2[100005];
|
||||
bool st1[100005], st2[100005];
|
||||
|
||||
void dijkstra1() {
|
||||
memset(dist1, 0x3f, sizeof(dist1));
|
||||
dist1[1] = 0;
|
||||
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
|
||||
q.push(make_pair(0, 1));
|
||||
while (!q.empty()) {
|
||||
auto t = q.top();
|
||||
q.pop();
|
||||
if (st1[t.second]) continue;
|
||||
for (auto i : g1[t.second]) {
|
||||
if (dist1[i.first] > t.first + i.second) {
|
||||
dist1[i.first] = t.first + i.second;
|
||||
q.push(make_pair(dist1[i.first], i.first));
|
||||
}
|
||||
}
|
||||
st1[t.second] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void dijkstra2() {
|
||||
memset(dist2, 0x3f, sizeof(dist2));
|
||||
dist2[1] = 0;
|
||||
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
|
||||
q.push(make_pair(0, 1));
|
||||
while (!q.empty()) {
|
||||
auto t = q.top();
|
||||
q.pop();
|
||||
if (st2[t.second]) continue;
|
||||
for (auto i : g2[t.second]) {
|
||||
if (dist2[i.first] > t.first + i.second) {
|
||||
dist2[i.first] = t.first + i.second;
|
||||
q.push(make_pair(dist2[i.first], i.first));
|
||||
}
|
||||
}
|
||||
st2[t.second] = true;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
cin >> u >> v >> w;
|
||||
g1[u].push_back(make_pair(v, w));
|
||||
g2[v].push_back(make_pair(u, w));
|
||||
}
|
||||
dijkstra1();
|
||||
dijkstra2();
|
||||
for (int i = 2; i <= n; i++) {
|
||||
ans += dist1[i] + dist2[i];
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user