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 every standard library
// Common file
#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<pll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<ld> vd;
typedef vector<vd> vvd;
typedef pair<ll,pll> ppll;
double EPS = 1e-9;
int INF = 2000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 };
#define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (ll(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (ll(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define RESET(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ef emplace_front
#define tiii tuple<int,int,int>
#define mt make_tuple
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define MAX 10000100
// #define mo 998244353
#define mo 1000000007
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
// Begin DEBUG //
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << '{' << p.first << ", " << p.second << '}';
}
template <class T, class = decay_t<decltype(*begin(declval<T>()))>,
class = enable_if_t<!is_same<T, string>::value>>
ostream &operator<<(ostream &os, const T &c) {
os << '[';
for (auto it = c.begin(); it != c.end(); ++it)
os << &", "[2 * (it == c.begin())] << *it;
return os << ']';
}
//support up to 5 args
#define _NTH_ARG(_1, _2, _3, _4, _5, _6, N, ...) N
#define _FE_0(_CALL, ...)
#define _FE_1(_CALL, x) _CALL(x)
#define _FE_2(_CALL, x, ...) _CALL(x) _FE_1(_CALL, __VA_ARGS__)
#define _FE_3(_CALL, x, ...) _CALL(x) _FE_2(_CALL, __VA_ARGS__)
#define _FE_4(_CALL, x, ...) _CALL(x) _FE_3(_CALL, __VA_ARGS__)
#define _FE_5(_CALL, x, ...) _CALL(x) _FE_4(_CALL, __VA_ARGS__)
#define FOR_EACH_MACRO(MACRO, ...) \
_NTH_ARG(dummy, ##__VA_ARGS__, _FE_5, _FE_4, _FE_3, _FE_2, _FE_1, _FE_0) \
(MACRO, ##__VA_ARGS__)
//Change output format here
#define out(x) #x " = " << x << "; "
#define debug(...) \
cout << "Line " << __LINE__ << ": " FOR_EACH_MACRO(out, __VA_ARGS__) << "\n"
// End DEBUG //
ll add(ll a, ll b, ll c = mo) {ll res=(a+b)%c; return res;}
ll mod_neg(ll a, ll b, ll c = mo) {ll res=(a-b+c)%c; return res;}
ll mul(ll a, ll b, ll c = mo) {ll res=(a*b)%c; res=(res+c)%c; return res;}
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
// If hash1 == hash2, their XOR is zero.
return hash1;
}
};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int MOD = 1000000007;
struct mi {
int v;
explicit operator int() const { return v; }
mi() { v = 0; }
mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi &operator+=(mi &a, mi b) {
if ((a.v += b.v) >= MOD) a.v -= MOD;
return a;
}
mi &operator-=(mi &a, mi b) {
if ((a.v -= b.v) < 0) a.v += MOD;
return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); }
mi &operator*=(mi &a, mi b) { return a = a * b; }
mi pow(mi a, long long p) {
assert(p >= 0);
return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator/(mi a, mi b) { return a * inv(b); }
typedef vector<mi> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
// Clear the stringstream
// ss.str("");
// ss.clear(); // Clear the stringstream state
// ss.str(str2);
///////////////////////////////////////////
/* Main starts here -> Mukund Krishnatrey*/
///////////////////////////////////////////
bool isValid(ll n, ll m, ll r, ll c){
if(r>=0 && r<n && c>=0 && c<m) return true;
else return false;
}
bool compf(pll p1, pll p2)
{
if(p1.ff!=p2.ff) return p1.ff>p2.ff;
else return p1.ss>p2.ss;
}
bool comps(pll p1, pll p2)
{
if(p1.ss!=p2.ss) return p1.ss<p2.ss;
else return p1.ff<p2.ff;
}
bool comp_len(pll p1, pll p2)
{
if((p1.ss-p1.ff)!=(p2.ss-p2.ff)) return ((p1.ss-p1.ff)<(p2.ss-p2.ff));
else return (p1.ff<p2.ff);
}
bool comp_custom(pair<pll,ll> &p1,pair<pll,ll> &p2){
if(p1.ff.ss!=p2.ff.ss) return p1.ff.ss<p2.ff.ss;
else return p1.ff.ff<p2.ff.ff;
}
struct scomps2
{
bool operator()(const pll &p1, const pll &p2) const{
if(p1.ss!=p2.ss) return (p1.ss>p2.ss);
else return (p1.ff<p2.ff);
}
};
struct scomps
{
bool operator()(const pll &p1, const pll &p2) const{
if(p1.ff!=p2.ff) return (p1.ff<p2.ff);
else return (p1.ss<p2.ss);
}
};
struct scomp_cur
{
bool operator()(pair<pll,ll> p1,pair<pll,ll> p2) const{
if(p1.ff.ff!=p2.ff.ff) return p1.ff.ff<p2.ff.ff;
else return p1.ss<p2.ss;
}
};
// Segment Tree normal //
struct Segtree
{
vector <ll> t, lazy;
Segtree(ll n) {t.resize(4*n+5, 0);}
void combine(ll v, ll v1, ll v2)
{
t[v]=t[v1]+t[v2];
}
// void push(ll v)
// {
// t[2*v+1]+=lazy[v];
// lazy[2*v+1]+=lazy[v];
// t[2*v+2]+=lazy[v];
// lazy[2*v+2]+=lazy[v];
// lazy[v]=0;
// }
void build(vl &a, ll v, ll tl, ll tr)
{
if(tl==tr) t[v]=a[tl];
else
{
ll tm=tl+(tr-tl)/2;
build(a, 2*v, tl, tm);
build(a, 2*v+1, tm+1, tr);
combine(v, 2*v, 2*v+1);
}
}
void update(ll v, ll tl, ll tr, ll pos, ll new_val)
{
if (tl == tr) {
t[v]+=new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v] = t[v*2] + t[v*2+1];
}
}
void update(ll v, ll tl, ll tr, ll l, ll r, ll new_val)
{
// debug(v);debug(tl);debug(tr);debug(l);debug(r);cout<<endl;
if(l==tl && r==tr)
{
t[v]+=new_val;
lazy[v]+=new_val;
return;
}
else if(r<tl || l>tr) return;
else
{
// push(v);
ll tm=tl+(tr-tl)/2;
update(2*v+1, tl, tm, l, min(tm,r), new_val);
update(2*v+2, tm+1, tr, max(l,tm+1), r, new_val);
combine(v, 2*v+1, 2*v+2);
}
}
// tl & tr represent what part of array we are considering
// currently in the segment tree, and l-r is the actual
// segment for which the query is asked.
ll query(ll v, ll tl, ll tr, ll l, ll r)
{
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[v];
}
ll tm = (tl + tr) / 2;
return query(v*2, tl, tm, l, min(r, tm))
+ query(v*2+1, tm+1, tr, max(l, tm+1), r);
}
};
bool cycle=false;
void dfs_topo(vl *adj, vector <bool> &vis, vb &rec, stack<ll> &s, ll u){
if(vis[u]) return;
rec[u]=true;
vis[u]=true;
ll v;
rep(i,adj[u].size()){
v=adj[u][i];
if(!vis[v]){
dfs_topo(adj,vis,rec,s,v);
}
else if(rec[v]) cycle=true;
}
s.push(u);
rec[u]=false;
}
ll k;
set <pll> zero;
set <pll> zero_rev;
void djikstra(vll *adj, ll &n, ll s, vl &d, vl &p) {
d.assign(n+1, INFF);
p.assign(n+1, -1);
d[s] = 0;
set<pll> q;
q.insert({0, s});
ll u,v,w;
while (!q.empty()) {
auto pr = *(q.begin());
q.erase(q.begin());
w=pr.ff; u=pr.ss;
if(w!=d[u]) continue;
for (auto edge : adj[u]) {
ll v = edge.first;
ll len = edge.second;
if ((d[u] + len) < d[v]) {
q.erase({d[v], v});
d[v] = d[u] + len;
p[v] = u;
q.insert({d[v], v});
}
}
}
}
void djikstra_two(vll *adj, ll &n, ll s, vvvl &d) {
d[s][0][0]=0;
// ff.ff -> distance, ff.ss -> 0's touched or not, ss -> vertice
set <pair<pll,pll>> q;
q.emplace(mp(0,0),mp(0,s));
ll w,t,u,v,nw,prev;
ll nt,next;
while(!q.empty()){
auto p=*(q.begin()); q.erase(q.begin());
w=p.ff.ff; t=p.ff.ss; prev=p.ss.ff; u=p.ss.ss;
// debug(u,w,t,prev);
if(w!=d[u][t][prev]) continue;
if(t){
for(auto np:adj[u]){
v=np.ff; nw=np.ss;
if(zero.find(mp(u,v))==zero.end()){
if(d[u][t][prev]+nw < d[v][t][prev]){
q.erase(mp(mp(d[v][t][prev],t),mp(prev,v)));
d[v][t][prev]=d[u][t][prev]+nw;
q.insert(mp(mp(d[v][t][prev],t),mp(prev,v)));
}
}
}
}
else{
for(auto np:adj[u]){
v=np.ff; nw=np.ss;
if(zero.find(mp(u,v))==zero.end()){
if(prev==1) nt=1;
else nt=0;
next=0;
}
else {
next=1;
nw=0;
nt=0;
}
if(d[u][t][prev]+nw < d[v][nt][next]){
q.erase(mp(mp(d[v][nt][next],nt),mp(next,v)));
d[v][nt][next]=d[u][t][prev]+nw;
q.insert(mp(mp(d[v][nt][next],nt),mp(next,v)));
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("fortmoo.in", "r")) {
freopen("fortmoo.in", "r", stdin);
freopen("fortmoo.out", "w", stdout);
}
cout<<fixed<<setprecision(10);
// ll t_t; cin>>t_t;
// while(t_t--){
ll n,m,s,t,u,v,x,y,w;
cin>>n>>m>>s>>t>>u>>v;
vll adj[n+1];
vector <pair<pll,ll>> edg;
rep(i,m){
cin>>x>>y>>w;
adj[x].eb(y,w);
adj[y].eb(x,w);
edg.eb(mp(x,y),w);
}
vl ds,dt,p;
djikstra(adj,n,s,ds,p);
djikstra(adj,n,t,dt,p);
zero.clear();
for(auto e:edg){
x=e.ff.ff; y=e.ff.ss; w=e.ss;
if(ds[x]+dt[y]+w == ds[t]){
// debug(x,y);
zero.emplace(x,y);
}
else if(ds[y]+dt[x]+w == ds[t]){
zero.emplace(y,x);
}
}
// debug(zero);
vvvl d(n+1,vvl(2, vl(2,INFF)));
djikstra_two(adj,n,u,d);
ll ans=INFF;
rep(i,2){
rep(j,2) ans=min(ans,d[v][i][j]);
}
// Reversing shortest path edges.
d.clear();
d.resize(n+1, vvl(2, vl(2,INFF)));
zero_rev.clear();
for(auto e:zero) zero_rev.emplace(e.ss,e.ff);
zero.clear();
zero.insert(all(zero_rev));
// debug(zero);
// debug(d);
djikstra_two(adj,n,u,d);
rep(i,2){
rep(j,2) ans=min(ans,d[v][i][j]);
}
cout<<ans<<endl;
// }
return 0;
}
// <------ Approaches to keep in mind during problems ------>
// ** If binary search applicable note it down.
// ** DP
// ** Stack on 2d graphs
// <------ For random mistakes during problems ------>
// ** Don't use bitwise operators in conditional true and false checks.
// ** Always assert for sanity checks if value should be in a certain range.
// ** In getting n^p, power p shouldn't be reduced by mod. So when obtaining the value of p, don't use any mod operations.
// ** Instead of -1, INT_MIN, etc. use INFF and -INFF if long long values are allowed.
// Consider possible values if >INT_MAX ( or < INT_MIN) don't use it.
// Take care that when erasing values from vector/array/related containers, that the iterator position is gone after that. And even in map/set/multiset, that iterator has to be replaced, can't do it-- or it++ after erase.
// When comparing values, for eg:-, two vectors of equal size, if every value is equal, return false or return v[0]<v[1]. If you return true, then you get segmentation fault.
// Multiset value erase will remove all instances of that value, better to use a map
// Try to divide problem into subcases, and deal with them independently.
// If lexicographical, then definitely don't do any swaps in your code, as it will change the order
// Please read constraints carefully, you might end up overcomplicating the question if not.
// Always verify your solution with atleast the given examples, and some extra edge cases you can think of, before you start coding it
// Have a pseduo structure of code written down, (or in your mand), before writing the same
// Write code in batches, keep testing every independent function, before writing next part
// Check for long long, too many mods
// Always take a look at the examples if stuck, or can't come with a solution for a while, normally examples contain a hint.
// <------ For finding solution to adhoc problem ------>
// ** Draw lots of small cases to gain a better understanding of the problem. If you're having trouble debugging, draw more cases. If you don't know how to start with a problem, draw more cases. Whenever you don't know how to further approach a problem, you're probably massing an important observation, so draw more cases and make observations about properties of the problem.
// ** Whenever you find an observation that seems useful, write it down! Writing down ideas lets you easily come back to them later, and makes sure you don't forget about ideas that could potentially be the solution.
// ** Don't get stuck on any specific idea, unless you see an entire solution.
// ** Try to approach the problem from a lot of different perspectives. Try to mess around with formulas or draw a visual depiction of the problem. One of the most helpful things you can do when solving ad hoc problems is to keep trying ideas until you make progress. This is something you get better at as you do more problems.
Compilation message (stderr)
commuter_pass.cpp: In function 'void dfs_topo(vl*, std::vector<bool>&, vb&, std::stack<long long int>&, ll)':
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:323:5: note: in expansion of macro 'rep'
323 | rep(i,adj[u].size()){
| ^~~
commuter_pass.cpp:39:44: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ~~~~^~~~~
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:323:5: note: in expansion of macro 'rep'
323 | rep(i,adj[u].size()){
| ^~~
commuter_pass.cpp: In function 'void djikstra(vll*, ll&, ll, vl&, vl&)':
commuter_pass.cpp:344:10: warning: unused variable 'v' [-Wunused-variable]
344 | ll u,v,w;
| ^
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:428:9: note: in expansion of macro 'rep'
428 | rep(i,m){
| ^~~
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:452:9: note: in expansion of macro 'rep'
452 | rep(i,2){
| ^~~
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:453:13: note: in expansion of macro 'rep'
453 | rep(j,2) ans=min(ans,d[v][i][j]);
| ^~~
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:466:9: note: in expansion of macro 'rep'
466 | rep(i,2){
| ^~~
commuter_pass.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
| ^
commuter_pass.cpp:45:19: note: in expansion of macro 'FOR'
45 | #define rep(i, n) FOR(i, 0, n)
| ^~~
commuter_pass.cpp:467:13: note: in expansion of macro 'rep'
467 | rep(j,2) ans=min(ans,d[v][i][j]);
| ^~~
commuter_pass.cpp:418:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
418 | freopen("fortmoo.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:419:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
419 | freopen("fortmoo.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... |