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>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=1e5+5;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2);
}
//=======================================================================================
int n, m, s, t, u, v;
struct edge{
int st, en;
int w;
};
vector<edge> ing[maxn];
vector<int> graph[maxn];
vector<edge> E(2*maxn);
vector<int> dis(maxn), rdis(maxn), dis3(maxn), dis4(maxn);
vector<bool> ds1(maxn), ds2(maxn), ds3(maxn), ds4(maxn), seen(maxn);
vector<int> indeg(maxn);
void dij1(){
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, s});
//dis[s]=0;
//ds1[s]=1;
while(pq.size()){
pii x=pq.top(); pq.pop();
if (ds1[x.s]) continue;
ds1[x.s]=1;
dis[x.s]=x.f;
REP(i,SZ(ing[x.s])){
pq.push({x.f+ing[x.s][i].w, ing[x.s][i].en});
}
}
}
void dij2(){
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, t});
//rdis[t]=0;
//ds2[t]=1;
while(pq.size()){
pii x=pq.top(); pq.pop();
if (ds2[x.s]) continue;
ds2[x.s]=1;
rdis[x.s]=x.f;
REP(i,SZ(ing[x.s])){
pq.push({x.f+ing[x.s][i].w, ing[x.s][i].en});
}
}
}
void dij3(){
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, u});
//ds3[u]=1;
while(pq.size()){
pii x=pq.top(); pq.pop();
if (ds3[x.s]) continue;
ds3[x.s]=1;
dis3[x.s]=x.f;
REP(i,SZ(ing[x.s])){
pq.push({x.f+ing[x.s][i].w, ing[x.s][i].en});
}
}
}
void dij4(){
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, v});
//ds3[u]=1;
while(pq.size()){
pii x=pq.top(); pq.pop();
if (ds4[x.s]) continue;
ds4[x.s]=1;
dis4[x.s]=x.f;
REP(i,SZ(ing[x.s])){
pq.push({x.f+ing[x.s][i].w, ing[x.s][i].en});
}
}
}
signed main (){
IOS();
cin>>n>>m>>s>>t>>u>>v;
REP(i,m){
cin>>E[i].st>>E[i].en>>E[i].w;
ing[E[i].st].pb({E[i].st, E[i].en, E[i].w});
ing[E[i].en].pb({E[i].en, E[i].st, E[i].w});
}
dij1();
dij2();
dij3();
dij4();
REP(i,m){
if (dis[E[i].st]+rdis[E[i].en]+E[i].w==dis[t]){
graph[E[i].st].pb(E[i].en);
indeg[E[i].en]++;
}
if (dis[E[i].en]+rdis[E[i].st]+E[i].w==dis[t]){
graph[E[i].en].pb(E[i].st);
indeg[E[i].st]++;
}
}
int ans=dis3[v];
queue<int> qu;
vector<pii> dp(n+1);
REP1(i,n){
if (indeg[i]==0) {
qu.push(i);
seen[i]=1;
}
dp[i].f=dis3[i];
dp[i].s=dis4[i];
}
while(qu.size()){
int x=qu.front(); qu.pop();
ans=min({ans, dp[x].f+dis4[x], dp[x].s+dis3[x]});
REP(i,SZ(graph[x])){
if (seen[graph[x][i]]) continue;
indeg[graph[x][i]]--;
dp[graph[x][i]].f=min(dp[graph[x][i]].f, dp[x].f);
dp[graph[x][i]].s=min(dp[graph[x][i]].s, dp[x].s);
if (indeg[graph[x][i]]==0){
qu.push(graph[x][i]);
seen[graph[x][i]]=1;
}
}
}
cout<<ans<<endl;
}
/*
8 9
1 5
2 6
1 2 5
2 3 5
3 4 5
4 5 15
5 6 5
6 7 5
7 8 5
8 1 15
4 8 1
*/
# | 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... |