This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>
using namespace std;
#define int long long
const int N = 1e5 + 7;
const int INF = 1e18 + 239;
int n;
vector<pair<int, int>> g[N];
vector<int> dj(int st) {
vector<int> d(n, INF);
set<pair<int, int>> s;
d[st] = 0;
s.insert({d[st], st});
while (!s.empty()) {
int v = s.begin()->second;
s.erase(s.begin());
for (auto [u, w] : g[v]) {
if (d[u] > d[v] + w) {
s.erase({d[u], u});
d[u] = d[v] + w;
s.insert({d[u], u});
}
}
}
return d;
}
vector<pair<int, int>> ng[N];
vector<int> ds;
vector<int> dt;
int s, t;
void init() {
ds = dj(s);
dt = dj(t);
for (int i = 0; i < n; i++) {
for (auto [u, w] : g[i]) {
if (ds[i] + w + dt[u] == ds[t]) {
ng[i].push_back({u, w});
} else {
// ng[i].push_back({u, w});
}
}
}
}
int dp[N];
vector<int> du;
vector<int> dv;
int calc(int x) {
if (dp[x] == -1) {
dp[x] = dv[x];
for (auto [t, w] : ng[x]) {
dp[x] = min(dp[x], calc(t));
}
}
return dp[x];
}
int solve(int u, int v) {
du = dj(u);
dv = dj(v);
fill(dp, dp + n, -1);
int ans = du[v];
for (int i = 0; i < n; i++) {
ans = min(ans, du[i] + calc(i));
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> n >> m;
int u, v;
cin >> s >> t >> u >> v;
s--;
t--;
u--;
v--;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
init();
int ans = min(solve(u, v), solve(v, u));
cout << ans << endl;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'long long int calc(long long int)':
commuter_pass.cpp:79:18: warning: unused variable 'w' [-Wunused-variable]
for (auto [t, w] : ng[x]) {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |