이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 10000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define single_case solve();
#define line cerr<<"----------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); }
#define mod 1000000007LL
const int N = 2e5 + 5;
vector<pair<ll, ll> > g[N];
ll ds[N], dt[N], du[N], dv[N];
ll n, m, s, t, u, v;
int vidjen[N];
ll d[N];
void dijkstra(ll src, ll dis[])
{
for(int i = 0;i<N;i++) dis[i] = LINF, vidjen[i] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > pq;
dis[src] = 0;
pq.push(make_pair(0, src));
while(len(pq))
{
ll u = pq.top().second;
pq.pop();
if(vidjen[u]) continue;
vidjen[u] = 1;
for(auto x : g[u])
{
ll v = x.first;
ll c = x.second;
if(dis[v]>dis[u]+c)
{
dis[v] = dis[u] + c;
pq.push(make_pair(dis[v], v));
}
}
}
}
void dijkstra2(ll src, ll dis[])
{
for(int i = 0;i<N;i++) dis[i] = LINF, vidjen[i] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > pq;
dis[src] = 0;
pq.push(make_pair(0, src));
while(len(pq))
{
ll u = pq.top().second;
pq.pop();
if(vidjen[u]) continue;
vidjen[u] = 1;
for(auto x : g[u])
{
ll v = x.first;
ll c = x.second;
if(dis[v]>dis[u]+c)
{
dis[v] = dis[u] + c;
if(ds[v]+dt[v]==ds[t]) continue;
pq.push(make_pair(dis[v], v));
}
}
}
}
bool cmp(int a, int b)
{
return du[a] < du[b];
}
void dfs(ll u, ll x)
{
vidjen[u] = 1;
du[u] = min(du[u], x);
for(auto W : g[u])
{
if(vidjen[W.first]==1||ds[W.first]+dt[W.first]!=ds[t]) continue;
dfs(W.first, x);
}
}
int main()
{
ios
cin>>n>>m>>s>>t>>u>>v;
for(int i = 0;i<m;i++)
{
ll a, b, c;
cin>>a>>b>>c;
g[a].pb(make_pair(b, c));
g[b].pb(make_pair(a, c));
}
dijkstra(s, ds);
dijkstra(t, dt);
if(ds[u]+dt[u]!=ds[t]) dijkstra2(u, du);
dijkstra2(v, dv);
vector<ll> w;
for(ll i = 1;i<=n;i++)
{
if(ds[i]+dt[i]==ds[t]) w.pb(i);
}
for(ll i = 0;i<N;i++) vidjen[i] = 0;
sort(all(w), cmp);
vector<ll> q;
for(ll i = 1;i<=n;i++) if(ds[i]+dt[i]==ds[t]) q.pb(i);
for(ll x : w)
{
if(vidjen[x]) continue;
dfs(x, du[x]);
}
dijkstra(v, d);
ll res = d[u];
for(int i = 1;i<=n;i++)
if(ds[i]+dt[i]==ds[t]) res = min(res, dv[i]+du[i]);
cout<<res;
return 0;
}
# | 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... |