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;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
void setIO(string name) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const ll LINF = 1e18;
const int N = 1e5+5;
int n, m, source[4];
ll ans(LINF), dist[4][N], dp[N][2];
vector<ii> 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<ll, int>> pq;
pq.push({0, source[kth]});
while (!pq.empty()) {
auto top = pq.top(); pq.pop();
ll to = -top.fi;
int u = top.se;
if (to != dist[kth][u]) continue;
fore(e, graph[u]) {
ll cost = to + e.se;
if (dist[kth][e.fi] > cost) {
dist[kth][e.fi] = cost;
pq.push({-cost, e.fi});
}
}
}
}
void dfs(int u) {
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] < LINF) {
ans = min(ans, dp[u][0] + dist[3][u]);
}
if (dp[u][1] < LINF) {
ans = min(ans, dp[u][1] + dist[2][u]);
}
}
int main() {
setIO("");
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);
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void setIO(std::string)':
commuter_pass.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |