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>
#define int long long
#define moony long long
#define pii pair<int, int>
#define fi first
#define se second
#define ld long double
#define vi vector<int>
#define vii vector<vector<int>>
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define per(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
using namespace std;
const int MOD = 1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int mul(int a, int b) {
(a *= b) %= MOD;
return a;
}
int ceil(int x, int y) {
return (x + y - 1) / y;
}
int bin_pow(int x, int y) {
int res=1;
while(y){if(y&1)res=res*x%MOD;x=x*x%MOD;y>>=1;}
return res;
}
const int INF = 1e17;
const int maxn = 1e6 + 5;
int n, m, S, T, U, V, ans;
vector<int> trace[maxn + 5];
vector<pair<int, int>> g[maxn + 5];
vector<int> dijkstra(int s) {
vector<int> dist(n + 1, -1);
dist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({dist[s], s});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != dist[u]) continue;
for (auto [v, cost] : g[u]) {
if (dist[v] == -1 || d + cost < dist[v]) {
dist[v] = d + cost;
pq.push({dist[v], v});
}
}
}
return dist;
}
vector<int> trace_dijkstra(int s) {
vector<int> dist(n + 1, -1);
dist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({dist[s], s});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != dist[u]) continue;
for (auto [v, cost] : g[u]) {
if (dist[v] == -1 || d + cost < dist[v]) {
dist[v] = d + cost;
trace[v].clear();
trace[v].push_back(u);
pq.push({dist[v], v});
} else if (dist[v] == d + cost) {
trace[v].push_back(u);
}
}
}
return dist;
}
void solve(int tc) {
cin >> n >> m;
cin >> 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<int> du = dijkstra(U);
vector<int> dv = dijkstra(V);
trace_dijkstra(S);
ans = du[V];
vector<int> dist(n + 1, -1);
dist[T] = dv[T];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({dist[T], T});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != dist[u]) continue;
ans = min(ans, du[u] + dist[u]);
for (int v : trace[u]) {
if (dist[v] == -1 || dist[v] > min(dist[u], dv[u])) {
dist[v] = min(dist[u], dv[u]);
pq.push({dist[v], v});
}
}
}
for (int i = 0; i <= n; ++i) dist[i] = -1;
dist[T] = du[T];
pq.push({dist[T], T});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != dist[u]) continue;
ans = min(ans, dv[u] + dist[u]);
for (int v : trace[u]) {
if (dist[v] == -1 || dist[v] > min(dist[u], du[u])) {
dist[v] = min(dist[u], du[u]);
pq.push({dist[v], v});
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) {
solve(i);
}
}
# | 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... |