This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
__author__: FireGhost
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
#define fi first
#define se second
const int N = 1e5 + 3;
int n, m, S, T, U, V;
vector<pii> g[N];
ll dS[N], dU[N], dV[N];
vector<int> prv[N], nxt[N];
bool vst[N];
ll dp[N], dp2[N];
void dijkstra(int s, ll* d) {
priority_queue<pli, vector<pli>, greater<pli>> pq;
d[s] = 0;
pq.emplace(0, s);
while (!pq.empty()) {
auto x = pq.top(); pq.pop();
int u = x.se;
ll dist = x.fi;
if (dist != d[u]) continue;
for (pair<int, int> e: g[u]) {
int v = e.fi, w = e.se;
if (d[v] > dist + w) {
d[v] = dist + w;
pq.emplace(d[v], v);
if (s == S) {
prv[v].clear();
prv[v].push_back(u);
}
}
else if (s == S && d[v] == dist + w) prv[v].push_back(u);
}
}
}
void dfs(int u) {
vst[u] = true;
for (int v: prv[u]) {
nxt[v].push_back(u);
if (!vst[v]) dfs(v);
}
}
void dfs_dp(int u) {
dp[u] = dV[u];
for (int v: nxt[u]) {
if (dp[v] == LLONG_MAX) dfs_dp(v);
dp[u] = min(dp[u], dp[v]);
}
}
void dfs_dp2(int u) {
dp2[u] = dV[u];
for (int v: prv[u]) {
if (dp2[v] == LLONG_MAX) dfs_dp2(v);
dp2[u] = min(dp2[u], dp2[v]);
}
}
void init() {
cin >> n >> m >> S >> T >> U >> V;
for (int i = 0; i < m; ++i) {
int u, v, w; cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
}
void solve() {
init();
memset(dS, 0x3f, sizeof dS);
memset(dU, 0x3f, sizeof dU), memset(dV, 0x3f, sizeof dV);
dijkstra(S, dS);
dijkstra(U, dU), dijkstra(V, dV);
dfs(T);
for (int i = 1; i <= n; ++i) {
dp[i] = dp2[i] = LLONG_MAX;
if (!vst[i]) prv[i].clear();
}
dfs_dp(S);
dfs_dp2(T);
ll ans = dU[V];
for (int i = 1; i <= n; ++i) {
if (!vst[i]) continue;
ans = min(ans, dU[i] + min(dp[i], dp2[i]));
}
cout << ans;
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
solve();
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... |