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<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5 + 5;
const ll inf = 1e18;
typedef pair<int, ll> edge;
#define fi first
#define se second
struct Node {
ll fu;
int u;
bool hu, hv;
bool operator < (const Node& oth) const {
return fu > oth.fu;
}
};
int n, m;
int S, T, U, V;
ll f[N][2][2], ans;
vector<edge> g[N];
vector<int> pa[N];
vector<ll> du, dv, ds, dt;
vector<ll> Dij (int st) {
for (int i = 1; i <= n; i++) pa[i].clear();
vector<ll> d(n + 1, inf);
d[st] = 0;
priority_queue<pair<ll, int>> pq;
pq.push({0, st});
while (!pq.empty()) {
ll du = -pq.top().fi;
int u = pq.top().se;
pq.pop();
if (d[u] != du) continue;
for (auto e : g[u]) {
int v = e.fi;
ll w = e.se;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
pq.push({-d[v], v});
pa[v] = {u};
}
else if (d[v] == d[u] + w) {
pa[v].push_back(u);
}
}
}
return d;
}
ll dp1[N], dp2[N];
bool vis[N];
void dfs (int i) {
vis[i] = 1;
dp1[i] = min(dp1[i], du[i]);
dp2[i] = min(dp2[i], dv[i]);
for (auto &j : pa[i]) {
if (!vis[j]) dfs(j);
dp1[i] = min(dp1[i], dp1[j]);
dp2[i] = min(dp2[i], dp2[j]);
}
ans = min(ans, dp1[i] + dv[i]);
ans = min(ans, dp2[i] + du[i]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> S >> T >> U >> V;
for (int i = 0; i < m; i++) {
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
du = Dij(U);
dv = Dij(V);
dt = Dij(T);
ds = Dij(S);
ans = du[V];
fill(begin(dp1), end(dp1), inf);
fill(begin(dp2), end(dp2), inf);
dfs(T);
cout << ans;
return 0;
}
# | 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... |