#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define sc second
#define pb push_back
#define int long long
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
template<typename type>
using ordered_set = tree<type, null_type, less<type>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 5, M = 2e5 + 5, mod = 1e9 + 7, inf = 1e18;
const int dl[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
const int ddl[] = {-1, -1, -1, 0, 1, 1, 1, 0}, ddc[] = {-1, 0, 1, 1, 1, 0, -1, -1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
int rng(int lo = 1, int hi = INT_MAX) {
uniform_int_distribution<int> rnd(lo, hi);
return rnd(gen);
}
struct mint {
int val;
mint(int32_t x = 0) {
val = x % mod;
}
mint(long long x) {
val = x % mod;
}
mint operator+(mint x) {
return val + x.val;
}
mint operator-(mint x) {
return val - x.val + mod;
}
mint operator*(mint x) {
return 1LL * val * x.val;
}
void operator+=(mint x) {
val = (*this + x).val;
}
void operator-=(mint x) {
val = (*this - x).val;
}
void operator*=(mint x) {
val = (*this * x).val;
}
friend auto operator>>(istream& in, mint &x) -> istream& {
in >> x.val;
x.val %= mod;
return in;
}
friend auto operator<<(ostream& out, mint const &x) -> ostream& {
out << x.val;
return out;
}
};
int n, m, s, t, u, v, ans, w[M], dst[3][N], dp[2][N];
bool vzt[N], taken[M], onpath[N];
vector<pii> g[N];
void dijk(int start, int st) {
for(int i=1; i<=n; i++)
dst[st][i] = inf;
dst[st][start] = 0;
priority_queue<pii, vector<pii>, greater<pii>> que;
que.push({0, start});
while(!que.empty()) {
auto [d, nod] = que.top();
que.pop();
if(d != dst[st][nod])
continue;
for(auto &[id, nxt] : g[nod])
if(d + w[id] < dst[st][nxt]) {
dst[st][nxt] = d + w[id];
que.push({dst[st][nxt], nxt});
}
}
}
void ohio() {
dijk(u, 1);
dijk(v, 2);
ans = min(ans, dst[1][v]);
memset(vzt, 0, sizeof(vzt));
for(int i=0; i<=n; i++)
dst[0][i] = dp[0][i] = dp[1][i] = inf;
dst[0][s] = 0;
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> que;
que.push({0, s, 0});
while(!que.empty()) {
auto [d, nod, par] = que.top();
que.pop();
if(d != dst[0][nod])
continue;
if(min(dst[1][nod], dp[0][par]) + min(dst[2][nod], dp[1][par]) < dp[0][nod] + dp[1][nod]) {
dp[0][nod] = min(dst[1][nod], dp[0][par]);
dp[1][nod] = min(dst[2][nod], dp[1][par]);
}
if(!vzt[nod]) {
vzt[nod] = 1;
for(auto &[id, nxt] : g[nod])
if(dst[0][nxt] >= d + w[id]) {
dst[0][nxt] = d + w[id];
que.push({dst[0][nxt], nxt, nod});
}
}
}
ans = min(ans, dp[0][t] + dp[1][t]);
}
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(0);
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
for(int i=1; i<=m; i++) {
int x, y;
cin >> x >> y >> w[i];
g[x].pb({i, y});
g[y].pb({i, x});
}
ans = inf;
ohio();
swap(s, t);
ohio();
cout << 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... |