# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48435 | aleksami | 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;
#define MAXN 100005
#define INF 1000000000000000
typedef pair<ll,ll> pii;
vector <pii> g[MAXN];
bool gotov[MAXN];
priority_queue <pii> pq;
int n,m;
void dijkstra(ll *d,int source)
{
for(int i = 0; i < MAXN; i++)d[i]=INF,gotov[i]=false;
d[source]=0;
pq.push({-d[source],source});
while(!pq.empty())
{
int v = pq.top().second;
pq.pop();
if(gotov[v])continue;
gotov[v]=1;
for(auto x:g[v])
{
int u = x.first;
int c = x.second;
if(d[u]>d[v]+c)d[u]=d[v]+c,pq.push({-d[u],u});
}
}
}
ll ds[MAXN];
ll dt[MAXN];
ll du[MAXN];
ll dv[MAXN];
ll d1[MAXN];
ll d2[MAXN];
int s,t;
void dijkstraa(ll *d,int *dd)
{
for(int i = 0; i < MAXN; i++)gotov[i]=false,dd[i]=d[i],pq.push({-dd[i],i});
while(!pq.empty())
{
int v = pq.top().second;
pq.pop();
if(gotov[v])continue;
gotov[v]=1;
for(auto x:g[v])
{
int u = x.first;
int c = x.second;
if( ds[u]+c+dt[v]==ds[t] && dd[u]>dd[v])dd[u]=dd[v],pq.push({-dd[u],u});
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int u,v;
cin >> n >> m >> s >> t >> u >> v;
for(int i = 0; i < m; i++)
{
int x,y,c;
cin >> x >> y >> c;
g[x].push_back({y,c});
g[y].push_back({x,c});
}
dijkstra(ds,s);
dijkstra(dt,t);
dijkstra(du,u);
dijkstra(dv,v);
dijkstraa(dv,d1);
dijkstraa(du,d2);
ll ans = INF;
for(int i = 1; i <= n; i++)
{
ans=min(ans,du[i]+d1[i]);
ans=min(ans,dv[i]+d2[i]);
}
cout << ans;
return 0;
}