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 ll long long
#define fi first
#define se second
#define pii pair<ll, ll>
const ll MAX = 2e5 + 10;
ll n, m;
ll s, t;
ll u, v;
ll x, y, w, w2;
vector <pii> veze[MAX];
ll dis[MAX][2];
ll sol[MAX][2];
ll arr[MAX];
ll trm[MAX];
queue <ll> q;
void distance(ll root, ll o) {
priority_queue <pii> q;
q.push({0, root});
dis[root][o] = 0;
while (!q.empty()) {
w = -q.top().fi;
x = q.top().se;
q.pop();
if (dis[x][o] != w) continue;
for (auto e : veze[x]) {
y = e.fi;
w2 = w + e.se;
if (dis[y][o] != -1 && dis[y][o] <= w2) continue;
dis[y][o] = w2;
q.push({-w2, y});
}
}
}
void bfs(ll root) {
priority_queue <pii> q;
q.push({0, root});
arr[root] = 0;
while (!q.empty()) {
w = -q.top().fi;
x = q.top().se;
q.pop();
if (arr[x] != w) continue;
for (auto e : veze[x]) {
y = e.fi;
w2 = w + e.se;
if (arr[y] != -1 && arr[y] <= w2) continue;
arr[y] = w2;
q.push({-w2, y});
}
}
}
void tramvaj(ll root) {
queue <ll> q;
q.push(root);
trm[root] = 1;
while (!q.empty()) {
x = q.front();
q.pop();
for (auto e : veze[x]) {
if (arr[e.fi] + e.se != arr[x]) continue;
trm[e.fi] = 1;
q.push(e.fi);
}
}
}
int main() {
cin >> n >> m;
cin >> s >> t >> u >> v;
memset(dis, -1, sizeof(dis));
memset(arr, -1, sizeof(arr));
for (ll i = 0; i < m; i++) {
cin >> x >> y >> w;
veze[x].push_back({y, w});
veze[y].push_back({x, w});
}
distance(u, 0);
distance(v, 1);
bfs(s);
tramvaj(t);
for (ll i = 1; i <= n; i++) {
if (!trm[i]) continue;
sol[i][0] = dis[i][0];
sol[i][1] = dis[i][1];
}
q.push(s);
while (!q.empty()) {
x = q.front();
q.pop();
for (auto e : veze[x]) {
if (!trm[e.fi]) continue;
if (arr[e.fi] != arr[x] + 1) continue;
q.push(e.fi);
sol[e.fi][0] = min(sol[e.fi][0], sol[x][0]);
}
}
q.push(t);
while (!q.empty()) {
x = q.front();
q.pop();
for (auto e : veze[x]) {
if (!trm[e.fi]) continue;
if (arr[e.fi] + 1 != arr[x]) continue;
q.push(e.fi);
sol[e.fi][1] = min(sol[e.fi][1], sol[x][1]);
}
}
ll rez = dis[v][0];
for (ll i = 1; i <= n; i++) {
if (!trm[i]) continue;
rez = min(rez, sol[i][0] + sol[i][1]);
}
cout << rez << "\n";
// cout << "U dis[" << u << "] = "; for (ll i = 1; i <= n; i++) cout << dis[i][0] << " "; cout << "\n";
// cout << "V dis[" << v << "] = "; for (ll i = 1; i <= n; i++) cout << dis[i][1] << " "; cout << "\n";
// cout << "\n";
//
//
// cout << "bfs -> "; for (ll i = 1; i <= n; i++) cout << arr[i] << " "; cout << "\n";
// cout << "trm -> "; for (ll i = 1; i <= n; i++) cout << trm[i] << " "; cout << "\n";
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... |