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>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "bus"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 2e5 + 9;
int n,m,s,t,u,v,dist[MAXN],trace[MAXN];
vector<ii> g[MAXN];
map<ii,int> specialedge;
void dijk(int s){
priority_queue<ii,vector<ii>,greater<ii>> pq;
pq.push({0,s});
memset(dist,0x3f,sizeof(dist));
dist[s] = 0;
trace[s] = s;
while(!pq.empty()){
int u = pq.top().se;
int du = pq.top().fi;
// if (s==3) cout<<u<<' '<<du<<endl;
pq.pop();
for(auto pt: g[u]){
int v = pt.fi;
int w = pt.se;
if (specialedge[ii(u,v)] == 1) {
w = 0;
// cout<<u<<' '<<v<<' '<<endl;
}
if (dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
trace[v] = u;
pq.push({dist[v],v});
}
}
}
}
int sub1[5003][5003];
void dijkstraN(int s){
memset(sub1[s],0x3f,sizeof(sub1[s]));
sub1[s][s] = 0;
priority_queue<ii,vector<ii>,greater<ii>> pq;
pq.push({0,s});
while(!pq.empty()){
int u = pq.top().se;
int du = pq.top().fi;
pq.pop();
for(auto pt: g[u]){
int v = pt.fi;
int w = pt.se;
if (sub1[s][v] > sub1[s][u] + w){
sub1[s][v] = sub1[s][u] + w;
pq.push({sub1[s][v],v});
}
}
}
}
void solvesub1(){
for(int i=1;i<=n;i++){
dijkstraN(i);
}
int ans = INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if (sub1[s][i] + sub1[i][j] + sub1[j][t] == sub1[s][t]){
// cout<<i<<' '<<j<<' '<<sub1[u][i]+sub1[j][v]<<endl;
ans = min({ans, sub1[u][i] + sub1[j][v], sub1[u][j] + sub1[v][i]});
}
}
// cout<<endl;
}
// cout<<endl;
cout<<ans<<endl;
}
main()
{
fast;
// freopen(TASKNAME".inp","r",stdin);
// freopen(TASKNAME".out","w",stdout);
cin>>n>>m;
cin>>s>>t;
cin>>u>>v;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
g[u].pb({v,w});
g[v].pb({u,w});
}
if (n<=5000){
solvesub1();
}
else{
dijk(s);
int tmp = t;
while(trace[tmp] != tmp){
int tr = trace[tmp];
// cout<<tr<<' '<<tmp<<endl;
specialedge[ii(tr,tmp)] = specialedge[ii(tmp,tr)] = 1;
tmp = trace[tmp];
}
// cout<<endl;
dijk(u);
cout<<dist[v]<<endl;
}
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijk(long long int)':
commuter_pass.cpp:33:13: warning: unused variable 'du' [-Wunused-variable]
33 | int du = pq.top().fi;
| ^~
commuter_pass.cpp: In function 'void dijkstraN(long long int)':
commuter_pass.cpp:60:13: warning: unused variable 'du' [-Wunused-variable]
60 | int du = pq.top().fi;
| ^~
commuter_pass.cpp: At global scope:
commuter_pass.cpp:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
89 | main()
| ^~~~
# | 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... |