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 task ""
#define fi first
#define se second
#define MASK(i) (1 << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const long long INF = 1e18;
const int MAX = 100100;
int n, m;
int S, T;
int U, V;
long long dpU[MAX];
long long dpV[MAX];
long long d[4][MAX];
vector<pair<int, int>> adj[MAX];
void dijkstra(int t, int s) {
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 1; i <= n; ++i) d[t][i] = INF;
pq.push({0, s});
d[t][s] = 0;
while (pq.size()) {
long long dist = pq.top().fi;
int u = pq.top().se;
pq.pop();
if (dist != d[t][u]) continue;
for (pair<int, int> &x: adj[u]) {
int v = x.fi, w = x.se;
if (minimize(d[t][v], dist + w))
pq.push({d[t][v], v});
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
cin >> S >> T;
cin >> U >> V;
for (int i = 1; i <= m; ++i) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
dijkstra(0, S); dijkstra(1, T);
dijkstra(2, U); dijkstra(3, V);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 1; i <= n; ++i) {
if (d[0][i] + d[1][i] == d[0][T])
pq.push({d[0][i], i});
}
for (int i = 1; i <= n; ++i) {
dpU[i] = dpV[i] = INF;
}
while (pq.size()) {
int i = pq.top().se;
pq.pop();
minimize(dpU[i], d[2][i]);
minimize(dpV[i], d[3][i]);
for (pair<int, int> &x: adj[i]) {
int j = x.fi, w = x.se;
if (d[0][i] + w + d[1][j] == d[0][T]) {
minimize(dpU[j], dpU[i]);
minimize(dpV[j], dpV[i]);
}
}
}
long long ans = d[2][V];
for (int i = 1; i <= n; ++i) {
if (d[0][i] + d[1][i] == d[0][T]) {
minimize(ans, dpU[i] + d[3][i]);
minimize(ans, dpV[i] + d[2][i]);
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:56:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:57:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen(task".out", "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... |