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;
template<typename T> bool ckmin(T& x, const T& y) { return x > y ? x = y, 1 : 0; }
template<typename T> bool ckmax(T& x, const T& y) { return x < y ? x = y, 1 : 0; }
const long long INF = (long long) 1e18;
const int MAX_NODE = (int) 1e5 + 5;
const int MAX_EDGE = (int) 2e5 + 5;
int n, m;
int S, T, U, V;
vector<pair<int, int>> adj[MAX_NODE];
struct Data {
int u;
long long w;
Data(int _u = 0, long long _w = 0) {
u = _u; w = _w;
}
bool operator < (const Data& other) const {
return w > other.w;
}
};
void dijkstra(int s, vector<long long>& dist) {
priority_queue<Data> Q;
Q.push(Data(s, dist[s] = 0));
while (!Q.empty()) {
auto [u, du] = Q.top(); Q.pop();
if (du != dist[u]) continue;
for (auto [v, dv] : adj[u]) {
long long cost = du + dv;
if (ckmin(dist[v], cost)) Q.push(Data(v, dist[v]));
}
}
}
namespace SUBTASK_1 {
void solve(void) {
vector<long long> distSU(n + 1, INF);
dijkstra(S, distSU);
vector<long long> distT(n + 1, INF);
dijkstra(T, distT);
vector<long long> distV(n + 1, INF);
dijkstra(V, distV);
long long ans = distV[U];
for (int i = 1; i <= n; ++i) if (distSU[i] + distT[i] == distSU[T]) ckmin(ans, distV[i]);
cout << ans << '\n';
}
}
namespace SUBTASK_3 {
const int MX = 303;
long long dist[MX][MX];
void solve(void) {
memset(dist, 0x3f, sizeof dist);
for (int i = 1; i <= n; ++i) {
dist[i][i] = 0;
for (auto [v, w] : adj[i]) dist[i][v] = dist[v][i] = w;
}
for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j)
ckmin(dist[i][j], dist[i][k] + dist[k][j]);
long long ans = dist[U][V];
for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j)
if (dist[S][i] + dist[i][j] + dist[j][T] == dist[S][T]) ckmin(ans, min(dist[V][i] + dist[j][U], dist[U][i] + dist[j][V]));
cout << ans << '\n';
}
}
namespace FULL {
vector<int> tree[MAX_NODE];
void solve(void) {
vector<long long> distS(n + 1, INF), distT(n + 1, INF), distU(n + 1, INF), distV(n + 1, INF);
dijkstra(S, distS); dijkstra(T, distT); dijkstra(U, distU); dijkstra(V, distV);
for (int u = 1; u <= n; ++u) for (auto [v, w] : adj[u]) {
if (distS[u] + w + distT[v] == distS[T]) tree[u].push_back(v);
}
long long ans = distU[V];
vector<vector<long long>> dp(n + 1, vector<long long>(2, INF));
vector<bool> vis(n + 1, false);
function<void(int)> dfs = [&](int u) {
vis[u] = true;
for (int v : tree[u]) {
if (!vis[v]) dfs(v);
ckmin(dp[u][0], min(dp[v][0], distU[v]));
ckmin(dp[u][1], min(dp[v][1], distV[v]));
}
if (dp[u][0] < INF) ckmin(ans, dp[u][0] + distV[u]);
if (dp[u][1] < INF) ckmin(ans, dp[u][1] + distU[u]);
};
dfs(S);
cout << ans << '\n';
}
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> S >> T >> U >> V;
for (int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
if (S == U) {
SUBTASK_1::solve();
return 0;
}
if (n <= 300) {
SUBTASK_3::solve();
return 0;
}
FULL::solve();
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstra(int, std::vector<long long int>&)':
commuter_pass.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | auto [u, du] = Q.top(); Q.pop();
| ^
commuter_pass.cpp:35:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for (auto [v, dv] : adj[u]) {
| ^
commuter_pass.cpp: In function 'void SUBTASK_3::solve()':
commuter_pass.cpp:69:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for (auto [v, w] : adj[i]) dist[i][v] = dist[v][i] = w;
| ^
commuter_pass.cpp: In function 'void FULL::solve()':
commuter_pass.cpp:90:44: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | for (int u = 1; u <= n; ++u) for (auto [v, w] : adj[u]) {
| ^
# | 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... |