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 all(x) x.begin(), x.end()
#define siz(x) int(x.size())
#define ll long long
#define ar array
#define vt vector
#define inf int(1e9)
#define lnf (long long) 1e12
const int nxm = int(1e5) + 7;
int n, m, s, t, u, v;
namespace sub1 {
vt<vt<ar<ll, 2>>> e(nxm);
vt<vt<ll>> d(3, vt<ll> (nxm, lnf));
void exe() {
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a, --b;
e[a].push_back({c, b});
e[b].push_back({c, a});
}
function<void(int, int)> dijkstra = [&](int root, int id) {
priority_queue<ar<ll, 2>, vt<ar<ll, 2>>, greater<ar<ll, 2>>> que;
que.push({0, root});
d[id][root] = 0;
while (que.size()) {
ll du = que.top()[0];
int u = que.top()[1];
que.pop();
if (du != d[id][u]) {
continue;
}
for (int i = 0; i < siz(e[u]); ++i) {
ll c = e[u][i][0];
int v = e[u][i][1];
if (d[id][v] > du + c) {
d[id][v] = du + c;
que.push({d[id][v], v});
}
}
}
};
dijkstra(s, 0);
dijkstra(t, 1);
dijkstra(v, 2);
ll ans = d[0][v];
for (int i = 0; i < n; ++i) {
if (d[0][t] != lnf && d[0][i] + d[1][i] == d[0][t]) {
ans = min(ans, d[2][i]);
}
}
cout << ans << "\n";
}
};
namespace sub3 {
ll e[305][305];
void exe() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
e[i][j] = lnf;
}
}
for (int i = 0; i < n; ++i) {
e[i][i] = 0;
}
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
cin >> e[a][b];
e[b][a] = e[a][b];
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (e[i][k] != lnf && e[k][j] != lnf) {
e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
}
}
}
}
ll ans = e[u][v];
//cout << e[4][0] + e[0][1] + e[1][6] << " " << e[4][6] << " " << e[5][1] << " " << e[0][7] << "\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (e[s][i] + e[i][j] + e[j][t] == e[s][t]) {
ans = min(ans, e[u][i] + e[j][v]);
ans = min(ans, e[u][j] + e[i][v]);
}
}
}
cout << ans << "\n";
}
};
int subtask() {
if (s == u) {
return 1;
} else if (n <= 300) {
return 3;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> t >> u >> v;
--s, --t, --u, --v;
if (subtask() == 1) {
sub1::exe();
} else if (subtask() == 3) {
sub3::exe();
}
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int subtask()':
commuter_pass.cpp:107:1: warning: control reaches end of non-void function [-Wreturn-type]
107 | }
| ^
# | 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... |