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
int n, m;
int s, t, u, v;
const int MXN = 100005;
vector<pair<int, int>> adj[MXN];
int distu[MXN], distv[MXN];
int dists[MXN], distt[MXN];
int dist[MXN];
bool access[MXN];
int result;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> proc;
void src(int no) {
for (int i=1; i<=n; i++) {
dist[i] = LLONG_MAX;
}
proc.push({0, no});
while (!proc.empty()) {
pair<int, int> tr = proc.top();
proc.pop();
int di = tr.first, no = tr.second;
if (dist[no] < di) continue;
dist[no] = di;
for (auto x: adj[no]) {
int ot = x.first;
int od = x.second + di;
if (dist[ot] > od) {
dist[ot] = od;
proc.push({od, ot});
}
}
}
}
int running_v[MXN];
int running_u[MXN];
bool byd(int a, int b) {
return dist[a] < dist[b];
}
void go(int from, int to) {
src(from);
vector<int> proc;
for (int i=1; i<=n; i++) proc.push_back(i);
sort(proc.begin(), proc.end(), byd);
for (auto no: proc) {
running_v[no] = distv[no];
running_u[no] = distu[no];
for (auto x: adj[no]) {
int ot = x.first;
int od = x.second;
if (dist[ot] + od == dist[no]) {
running_u[no] = min(running_u[no], running_u[ot]);
running_v[no] = min(running_v[no], running_v[ot]);
}
}
if (access[no]) {
result = min(result, running_u[no] + distv[no]);
result = min(result, distu[no] + running_v[no]);
}
}
}
signed main() {
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
for (int i=0; i<m; i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
src(u);
for (int i=1; i<=n; i++) distu[i] = dist[i];
src(v);
for (int i=1; i<=n; i++) distv[i] = dist[i];
result = distu[v];
src(s);
for (int i=1; i<=n; i++) dists[i] = dist[i];
src(t);
for (int i=1; i<=n; i++) distt[i] = dist[i];
for (int i=1; i<=n; i++) {
if (dists[i] + distt[i] == dists[t]) {
access[i] = true;
}
}
go(s, t);
go(t, s);
cout << result << endl;
}
# | 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... |