# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
937253 | Marco_Escandon | Commuter Pass (JOI18_commuter_pass) | C++11 | 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 x first
#define y second
vector<vector<ll>> v(5,vector<ll>(100001,1e16));
vector<vector<pair<ll,ll>>>G1(100001);
vector<vector<ll>>G2(100001);
void dijkstra(ll pl, ll node)
{
priority_queue<pair<ll,ll>> q;
q.push({0,node});
while(!q.empty())
{
pair<ll,ll> a=q.top();q.pop();
if(v[pl][a.y]==1e16)
{
v[pl][a.y]=-a.x;
for(auto i:G1[a.y])
if(v[pl][i.x]==1e16)
q.push({a.x-i.y,i.x});
}
}
}
pair<ll,pair<ll,ll>> dp(n,{-1,{0,0}});
pair<ll,pair<ll,ll>> dfs(ll node, ll padre)
{
if(dp[node].first!=-1)
return dp[node];
pair<ll,pair<ll,ll>> sol={v[2][node]+v[3][node],{v[2][node],v[3][node]}};
for(auto i:G2[node])
{
if(i!=padre)
{
pair<ll,pair<ll,ll>> temp=dfs(i,node);
sol.x=min(sol.x,temp.x);
sol.x=min(sol.x,v[2][node]+temp.y.y);
sol.x=min(sol.x,v[3][node]+temp.y.x);
sol.y.x=min(sol.y.x,temp.y.x);
sol.y.y=min(sol.y.y,temp.y.y);
}
}
return dp[node]=sol;
}
int main()
{
ll n,m;
cin>>n>>m;
ll s,t,u,w;
cin>>s>>t>>u>>w;
for(int i=0; i<m; i++)
{
ll a,b,c;
cin>>a>>b>>c;
G1[a].push_back({b,c});
G1[b].push_back({a,c});
}
dijkstra(0,s);
dijkstra(1,t);
dijkstra(2,u);
dijkstra(3,w);
for(int i=1; i<=n; i++)
for(auto j:G1[i])
if(v[0][i]+v[1][j.x]+j.y==v[0][t])
G2[i].push_back(j.x);
cout<<min(dfs(s,0).x,v[2][w]);
}