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 foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define fore(x, v) for(auto &x : v)
#define fi first
#define se second
using namespace std;
const long long INF = 1e18;
const int N = 1e5 + 5;
int n, m, source[4];
long long ans(INF), dist[4][N], dp[N][2], vi[N];
vector <pair <int, int> > graph[N];
vector <int> tree[N];
void dijkstra(int kth) {
memset(dist[kth], 0x3f, sizeof(dist[kth]));
dist[kth][source[kth]] = 0;
priority_queue <pair <long long, int> > pq;
pq.push({0, source[kth]});
while (!pq.empty()) {
auto top = pq.top(); pq.pop();
long long to = -top.fi;
int u = top.se;
if (to != dist[kth][u]) continue;
fore(e, graph[u]) {
long long cost = to + e.se;
if (dist[kth][e.fi] > cost) {
dist[kth][e.fi] = cost;
pq.push({-cost, e.fi});
}
}
}
}
void dfs(int u) {
if (vi[u]) return;
vi[u] = 1;
fore(v, tree[u]) {
dfs(v);
dp[u][0] = min(dp[u][0], min(dp[v][0], dist[2][v]));
dp[u][1] = min(dp[u][1], min(dp[v][1], dist[3][v]));
}
if (dp[u][0] < INF) {
ans = min(ans, dp[u][0] + dist[3][u]);
}
if (dp[u][1] < INF) {
ans = min(ans, dp[u][1] + dist[2][u]);
}
}
int main() {
cin >> n >> m;
foru(i, 0, 3) {
cin >> source[i];
}
foru(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
foru(i, 0, 3) {
dijkstra(i);
}
foru(i, 1, n) {
fore(e, graph[i]) {
int j = e.fi;
if (dist[0][i] + dist[1][j] + e.se != dist[0][source[1]]) continue;
tree[i].push_back(j);
}
}
memset(dp, 0x3f, sizeof(dp));
dfs(source[0]);
cout << min(dist[2][source[3]], ans);
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... |