이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 10000000000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
vector<pair<ll,ll>>graph[100001];
ll dist[100001],dist1[100001];
ll ans[100001][3];
ll par[100001][3];
map<dos,ll>weights;
ll tempi;
dos ki(ll u,ll v)
{
return {min(u,v),max(u,v)};
}
bool edgy(ll u,ll v)
{
if(dist[u]+weights[ki(u,v)]+dist1[v] == tempi)return true;
if(dist[v]+weights[ki(u,v)]+dist1[u] == tempi)return true;
return false;
}
bool edgy1(ll u,ll v,int lev)
{
if(dist[par[u][lev]]+weights[ki(par[u][lev],u)]+weights[ki(u,v)]+dist1[v] == tempi)return true;
//if(dist[par[][lev]]+weights[ki(par[u][lev],u)]+weights[ki(u,v)]+dist1[v] == tempi)
return false;
}
int main()
{
//flash;
ll n,m,s,t,u,v;
cin>>n>>m>>s>>t>>u>>v;
s--,t--,u--,v--;
for (ll i = 0; i < m; ++i)
{
ll x,y,z;
cin>>x>>y>>z;
x--,y--;
graph[x].pb({y,z});
graph[y].pb({x,z});
weights[ki(x,y)]=z;
}
for (ll i = 0; i < n; ++i)
{
dist[i]=INF;
}
priority_queue<dos,vedos,greater<dos>>hi;
hi.push({0,s});
dist[s]=0;
while(!hi.empty())
{
auto u=hi.top();
hi.pop();
if(dist[u.second]<u.first)continue;
for(auto it:graph[u.second])
{
if(u.first+it.second<dist[it.first])
{
dist[it.first]=u.first+it.second;
hi.push({dist[it.first],it.first});
}
}
}
tempi=dist[t];
fill(dist1,dist1+n,INF);
hi.push({0,t});
dist1[t]=0;
while(!hi.empty())
{
auto u=hi.top();
hi.pop();
if(dist1[u.second]<u.first)continue;
for(auto it:graph[u.second])
{
if(u.first+it.second<dist1[it.first])
{
dist1[it.first]=u.first+it.second;
hi.push({dist1[it.first],it.first});
}
}
}
for (ll i = 0; i < n; ++i)
{
ans[i][0]=ans[i][1]=ans[i][2]=INF;
}
priority_queue<pair<ll,dos>,vector<pair<ll,dos>>,greater<pair<ll,dos>>>hey;
ans[u][0]=0;
hey.push({0,{u,0}});
while(!hey.empty())
{
auto u=hey.top();
hey.pop();
auto node=u.second.first;
auto lev=u.second.second;
auto far=u.first;
if(ans[node][lev]<far)continue;
for(auto it:graph[node])
{
if(lev==0)
{
if(far+it.second<ans[it.first][0])
{
ans[it.first][0]=far+it.second;
hey.push({ans[it.first][0],{it.first,0}});
par[it.first][0]=node;
}
if(edgy(node,it.first) && far<ans[it.first][1])
{
ans[it.first][1]=far;
hey.push({ans[it.first][1],{it.first,1}});
par[it.first][1]=node;
}
}
else if(lev==1)
{
if(edgy1(node,it.first,lev) && far<ans[it.first][1])
{
ans[it.first][1]=far;
hey.push({ans[it.first][1],{it.first,1}});
par[it.first][1]=node;
}
if(far+it.second<ans[it.first][2])
{
ans[it.first][2]=far+it.second;
hey.push({ans[it.first][2],{it.first,2}});
par[it.first][2]=node;
}
}
else
{
if(far+it.second<ans[it.first][2])
{
ans[it.first][2]=far+it.second;
hey.push({ans[it.first][2],{it.first,2}});
par[it.first][2]=node;
}
}
}
}
cout<<min(ans[v][0],min(ans[v][1],ans[v][2]));
return 0;
}
//code the AC sol !
// BS/queue/map
컴파일 시 표준 에러 (stderr) 메시지
commuter_pass.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
commuter_pass.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
# | 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... |