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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second
#pragma GCC optimize("O3")
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace __gnu_pbds;
using namespace std;
typedef tree<long long, null_type, less_equal<long long>,
rb_tree_tag, tree_order_statistics_node_update> Tree;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 1e9 + 7; //[998244353, 1e9 + 7, 1e9 + 13]
const ld PI = acos(-1);
const ll NROOT = 500;
ll binpow(ll a, ll b, ll _MOD = -1) {
if (_MOD == -1)
_MOD = MOD;
ll res = 1;
for (; b; b /= 2, a *= a, a %= _MOD)
if (b & 1) res *= a, res %= _MOD;
return res;
}
void set_IO(string s) {
#ifndef LOCAL
string in = s + ".in";
string out = s + ".out";
freopen(in.c_str(), "r", stdin);
freopen(out.c_str(), "w", stdout);
#endif
}
bool dataOverflow(ll a, ll b) {return (log10(a) + log10(b) >= 18);}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
ll invmod(ll a) {return binpow(a, MOD - 2);}
#define int long long
int n, m, s, t, u, v;
vector<vector<ll>> dis;
vector<vector<pair<int, ll>>> g;
/*
0 -> s
1 -> t
2 -> u
3 -> v
*/
void Dijkstra(int start, int state) {
using T = pair<ll, int>;
priority_queue<T, vector<T>, greater<T>> q;
q.push({0, start});
while (!q.empty()) {
auto [d, x] = q.top();
q.pop();
if (dis[state][x] <= d)
continue;
dis[state][x] = d;
for (auto &y : g[x])
q.push({d + y.S, y.F});
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> s >> t >> u >> v;
g.resize(n + 1);
dis.resize(4, vector<ll> (n + 1, INF));
for (int i = 1; i <= m; i ++) {
int a, b; ll c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
Dijkstra(s, 0);
Dijkstra(t, 1);
Dijkstra(u, 2);
Dijkstra(v, 3);
queue<pair<int, ll>> q;
vector<bool> vis(n + 1);
vector<int> in1(n + 1), in2(n + 1);
vector<vector<pair<int, ll>>> g1(n + 1), g2(n + 1);
q.push({t, dis[0][t]});
while (!q.empty()) {
auto [x, d] = q.front();
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (auto &y : g[x]) {
if (dis[0][y.F] + y.S == dis[0][x]) {
q.push({y.F, dis[0][y.F]});
g1[y.F].push_back({x, y.S});
g2[x].push_back({y.F, y.S});
in1[x] ++;
in2[y.F] ++;
}
}
}
vector<vector<ll>> dp1(2, vector<ll> (n + 1, 1e18)); // Minimum distance to U
vector<vector<ll>> dp2(2, vector<ll> (n + 1, 1e18)); // Minimum distance to V
for (int i = 1; i <= n; i ++) {
dp1[0][i] = dp1[1][i] = dis[2][i];
dp2[0][i] = dp2[1][i] = dis[3][i];
}
q.push({s, 0});
q.push({t, 0});
while (!q.empty()) {
int x = q.front().F;
q.pop();
for (auto &y : g1[x]) {
dp1[0][y.F] = min(dis[2][y.F], dp1[0][x]);
dp2[1][y.F] = min(dis[3][y.F], dp2[1][x]);
in1[y.F] --;
if (in1[y.F] == 0)
q.push({y.F, 0});
}
}
q.push({s, 0});
q.push({t, 0});
while (!q.empty()) {
int x = q.front().F;
q.pop();
for (auto &y : g2[x]) {
dp1[1][y.F] = min(dis[2][y.F], dp1[1][x]);
dp2[0][y.F] = min(dis[3][y.F], dp2[0][x]);
in2[y.F] --;
if (in2[y.F] == 0)
q.push({y.F, 0});
}
}
for (int i = 1; i <= n; i ++) {
dp1[0][i] = min(dp1[0][i], dp1[1][i]);
dp2[0][i] = min(dp2[0][i], dp2[1][i]);
}
ll ans = dis[2][v];
for (int i = 1; i <= n; i ++)
ans = min(dp1[0][i] + dp2[0][i], ans);
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void Dijkstra(long long int, long long int)':
commuter_pass.cpp:77:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | auto [d, x] = q.top();
| ^
commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:117:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
117 | auto [x, d] = q.front();
| ^
commuter_pass.cpp: In function 'void set_IO(std::string)':
commuter_pass.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen(in.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:48:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen(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... |