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 int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> bool chmax(T& a, const T b) { return a<b ? a = b, 1 : 0; }
template <typename T> bool chmin(T& a, const T b) { return a>b ? a = b, 1 : 0; }
const int MAXN = 200005;
int n,m;
int s,t;
int u,v;
vector<pi> adj[MAXN];
int dist[MAXN][4];
int D1[MAXN], Dn[MAXN];
struct edge {
int u, v, c;
} E[MAXN];
typedef pair<int,pi> pii;
void dij(int source, int * dist) {
FOR(i,0,n)dist[i]=oo;
dist[source] = 0;
priority_queue<pi,vector<pi>,greater<pi>> pq;
pq.push({0, source});
while(pq.size()) {
pi c=pq.top(); pq.pop();
int x=c.s, d=c.f;
if(dist[x]!=d)continue;
for(auto v:adj[x]) {
int nd=v.s+d;
int nx=v.f;
if(dist[nx]>nd) {
dist[nx]=nd;
pq.push({nd,nx});
}
}
}
}
int32_t main()
{
IAMSPEED
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
FOR(i,1,m){
int a,b,c;cin>>a>>b>>c;
E[i]={a,b,c};
adj[a].pb({b,c});
adj[b].pb({a,c});
}
dij(s,D1);
dij(t,Dn);
FOR(i,0,n)FOR(j,0,3)dist[i][j]=oo;
priority_queue<pii,vector<pii>,greater<pii>>pq;
dist[u][0]=0;
pq.push({0,{u,0}}); // i haven't started anything yet.
while(pq.size()) {
pii c=pq.top(); pq.pop();
int d=c.f, x=c.s.f, y=c.s.s;
if(dist[x][y]!=d)continue;
for(auto v:adj[x]) {
int nx=v.f;
if(y == 1 || y == 0) { // increasing path
bool on=(D1[t] == D1[x] + v.s + Dn[nx]); // this path is part of commuter pass
if(on) {
if(chmin(dist[nx][1],dist[x][y])) {
pq.push({dist[nx][1], {nx, 1}});
}
}
if(y==0) {
if(chmin(dist[nx][0],dist[x][y] + v.s)) {
pq.push({dist[nx][0], {nx,0}});
}
} else {
if(chmin(dist[nx][3], dist[x][y] + v.s)) {
pq.push({dist[nx][3], {nx,3}});
}
}
} else if(y == 0 || y==2){
bool on=(Dn[s] == Dn[x] + v.s + D1[nx]);
if(on) {
if(chmin(dist[nx][2],dist[x][y])) {
pq.push({dist[nx][2], {nx, 2}});
}
}
{
if(y==0){
if(chmin(dist[nx][0],dist[x][y] + v.s)) {
pq.push({dist[nx][0], {nx,0}});
}
} else {
if(chmin(dist[nx][3],dist[x][y]+v.s)) {
pq.push({dist[nx][3], {nx, 3}});
}
}
}
} else {
if(chmin(dist[nx][y],dist[x][y] + v.s)) {
pq.push({dist[nx][y], {nx,y}});
}
}
}
}
cout << min({dist[v][0], dist[v][1], dist[v][2], dist[v][3]}) << '\n';
}
# | 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... |