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;
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];
vector<edge> g[N];
vector<ll> Dij (int st) {
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});
}
}
}
return d;
}
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});
}
vector<ll> ds = Dij(S);
vector<ll> du = Dij(U);
vector<ll> dv = Dij(V);
ll ans = du[V];
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 2; j++) {
for (int x = 0; x < 2; x++) {
f[i][j][x] = inf;
}
}
}
priority_queue<Node> pq;
pq.push({0, S, (S == U), (S == V)});
f[S][S == U][S == V] = 0;
while (!pq.empty()) {
auto [fu, u, hu, hv] = pq.top(); pq.pop();
if (fu != f[u][hu][hv]) continue;
// if (u == 5) {
// cout << "yes\n";
// }
for (auto [v, c] : g[u]) {
if (ds[v] == ds[u] + c) {
bool nhu = hu | (v == U);
bool nhv = hv | (v == V);
if (f[v][nhu][nhv] > fu) {
f[v][nhu][nhv] = fu;
pq.push({f[v][nhu][nhv], v, nhu, nhv});
}
if (!nhu) {
if (f[v][1][nhv] > fu + du[v]) {
f[v][1][nhv] = fu + du[v];
pq.push({f[v][1][nhv], v, 1, nhv});
}
}
if (!nhv) {
if (f[v][nhu][1] > fu + dv[v]) {
f[v][nhu][1] = fu + dv[v];
pq.push({f[v][nhu][1], v, nhu, 1});
}
}
if (!nhu && !nhv) {
if (f[v][1][1] > fu + du[v] + dv[v]) {
f[v][1][1] = fu + du[v] + dv[v];
pq.push({f[v][1][1], v, 1, 1});
}
}
}
}
}
ans = min(ans, f[T][1][1]);
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... |