mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-25 06:31:59 +00:00
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#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;
|
|
}
|