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;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int MXN = 100005;
int n, m, s, t, u, v, ds[MXN], du[MXN], dv[MXN], ans = LLONG_MAX, dp[2][MXN];
vector<pii> edge[MXN];
vector<int> L[MXN], R[MXN], father[MXN];
bitset<MXN> b;
void GET_PATH() {
b.reset();
priority_queue<piii, vector<piii>, greater<piii>> pq;
// pq.push({0, {s, s}});
b[s] = true;
ds[s] = 0;
for (auto &i : edge[s]) {
pq.push({i.second, {s, i.first}});
}
while (pq.size()) {
piii now = pq.top();
pq.pop();
if (b[now.second.second]) {
if (ds[now.second.second] == now.first) {
father[now.second.second].push_back(now.second.first);
}
continue;
}
b[now.second.second] = true;
ds[now.second.second] = now.first;
father[now.second.second].push_back(now.second.first);
for (auto &i : edge[now.second.second]) {
pq.push({now.first + i.second, {now.second.second, i.first}});
}
}
b.reset();
queue<int> q;
q.push(t);
while (q.size()) {
int now = q.front();
q.pop();
if (b[now]) continue;
b[now] = true;
for (auto &i : father[now]) {
L[now].push_back(i);
R[i].push_back(now);
if (b[i]) continue;
q.push(i);
}
}
}
void DIJKSTRA(int s, int *out) {
b.reset();
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, s});
while (pq.size()) {
pii now = pq.top();
pq.pop();
if (b[now.second]) continue;
b[now.second] = true;
out[now.second] = now.first;
for (auto &i : edge[now.second]) {
if (b[i.first]) continue;
pq.push({now.first + i.second, i.first});
}
}
}
void BFS() {
b.reset();
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, s});
while (pq.size()) {
pii now = pq.top();
pq.pop();
if (b[now.second]) continue;
b[now.second] = true;
dp[0][now.second] = LLONG_MAX;
dp[1][now.second] = LLONG_MAX;
for (auto &i : L[now.second]) {
dp[0][now.second] = min(dp[0][now.second], dp[0][i]);
dp[1][now.second] = min(dp[1][now.second], dp[1][i]);
}
dp[0][now.second] = min(dp[0][now.second], du[now.second]);
dp[1][now.second] = min(dp[1][now.second], dv[now.second]);
ans = min(ans, min(dp[0][now.second] + dv[now.second], dp[1][now.second] + du[now.second]));
for (auto &i : R[now.second]) {
if (b[i]) continue;
pq.push({ds[i], i});
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int x, y, z;
cin >> n >> m >> s >> t >> u >> v;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
edge[x].push_back({y, z});
edge[y].push_back({x, z});
}
GET_PATH();
DIJKSTRA(u, du);
DIJKSTRA(v, dv);
ans = du[v];
BFS();
cout << ans << endl;
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... |