# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
719713 | tigar | Commuter Pass (JOI18_commuter_pass) | C++14 | 0 ms | 0 KiB |
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;
typedef long long ll;
ll INF=LONG_LONG_MAX;
ll m, n, s, t, u, v;
vector<pair<ll, ll> >graff[200010];
vector<ll>dag[200010], dag1[200010];
bool check1[200010], check2[200010];
ll distu[200010], distv[200010], dists[200010], distt[200010];
ll res=INF;
void dijkstra(ll cvor, ll *dist)
{
for(int i=0; i<100010; i++){check1[i]=false; dist[i]=INF;}
priority_queue<pair<ll, ll> >ord;
dist[cvor]=0;
ord.push({0, cvor});
while(!ord.empty())
{
int cv=ord.top().second;
ord.pop();
if(check1[cv])continue;
check1[cv]=true;
for(int j=0; j<graff[cv].size(); j++)
{
ll pp=graff[cv][j].first;
dist[pp]=min(dist[pp], dist[cv]+graff[cv][j].second);
if(!check1[pp])ord.push({-dist[pp], pp});
}
}
}
void dfs1(ll prosli, ll cvor)
{
if(check2[cvor])return;
check2[cvor]=true;
dag[prosli].push_back(cvor);
dag1[cvor].push_back(prosli);
if(cvor==t)return;
for(int i=0; i<graff[cvor].size(); i++)
{
if(dists[cvor]+graff[cvor][i].second+distt[graff[cvor][i].first]==dists[t])dfs1(cvor, graff[cvor][i].first);
}
return;
}
void dfs2(ll cvor, ll minv, vector<ll>*dagg)
{
minv=min(minv, distv[cvor]);
res=min(res, minv+distu[cvor]);
if(cvor==t)return;
for(int i=0; i<dagg[cvor].size(); i++)
{
dfs2(dagg[cvor][i], minv);
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(); cout.tie();
cin>>n>>m;
cin>>s>>t;
cin>>u>>v;
ll a, b, c;
for(int i=0; i<m; i++)
{
cin>>a>>b>>c;
graff[a].push_back({b, c});
graff[b].push_back({a, c});
}
dijkstra(u, distu);
dijkstra(v, distv);
dijkstra(s, dists);
dijkstra(t, distt);
dists[0]=0;
dfs1(0, s);
dfs2(s, INF, dag);
dfs2(t, INF, dag1);
cout<<min(res, distv[u]);
return 0;
}