This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 2e5 + 7;
ll diss [N], disv [N], disu [N], dpv [N], dpu [N], n, m, S, T, V, U, ans;
priority_queue <pll, vector <pll>, greater <pll>> pq;
vector <ll> out [N], tops;
vector <pll> adj [N];
set <pll> st;
bool ok [N];
void dijkstra (ll v, ll dis []) {
memset(dis, 63, sizeof diss);
dis[v] = 0; st.insert({0, v});
while (!st.empty()) {
v = st.begin()->sc;
st.erase(st.begin());
for (auto [u, w] : adj[v]) if (dis[u] > dis[v] + w) st.erase({dis[u], u}), dis[u] = dis[v] + w, st.insert({dis[u], u});
}
}
void dfs (cl &v = S) {
for (auto &u : out[v]) dfs(u);
tops.push_back(v);
if (SZ(tops) > n) { cout<< ":obd:"; exit(0); }
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n >> m >> S >> T >> U >> V;
for (ll i = 0, v, u, w; i < m; i++) {
cin>> v >> u >> w;
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
dijkstra(S, diss);
dijkstra(V, disv);
dijkstra(U, disu);
ans = disv[U];
for (ll v = 1; v <= n; v++) for (auto [u, w] : adj[v]) if (diss[u] == diss[v] + w) out[v].push_back(u);
dfs();
ok[T] = 1;
for (auto &v : tops) {
dpv[v] = disv[v]; dpu[v] = disu[v];
for (auto &u : out[v]) ok[v] |= ok[u], dpv[v] = min(dpv[v], dpv[u]), dpu[v] = min(dpu[v], dpu[u]);
if (!ok[v]) dpv[v] = dpu[v] = inf;
ans = min({ans, disv[v] + dpu[v], disu[v] + dpv[v]});
}
if (ans >= inf) ans = -1;
cout<< ans << endl;
cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
6 6
1 6
1 4
1 2 1
2 3 1
3 5 1
2 4 3
4 5 2
5 6 1
6 5
1 2
3 6
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
8 8
5 7
6 8
1 2 2
2 3 3
3 4 4
1 4 1
1 5 5
2 6 6
3 7 7
4 8 8
5 5
1 5
2 3
1 2 1
2 3 10
2 4 10
3 5 10
4 5 10
10 15
6 8
7 9
2 7 12
8 10 17
1 3 1
3 8 14
5 7 15
2 3 7
1 10 14
3 6 12
1 5 10
8 9 1
2 9 7
1 4 1
1 8 1
2 4 7
5 6 16
*/
# | 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... |