이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef thuc
freopen("input.INP","r",stdin);
freopen("output.OUT","w",stdout);
#endif
}
const ll N=2e5+10,inf=1e18;
ll dists[N],distt[N],n,m,f[N][4],U[N],V[N],C[N];
vector<pll> gr[N];
void dijkstra(ll src,ll dist[])
{
priority_queue<pll,vector<pll>,greater<pll> > qq;
for(int i=1;i<=n;i++) dist[i]=inf;
dist[src]=0;
qq.emplace(0,src);
while(qq.size())
{
auto [d,u]=qq.top();
qq.pop();
if(dist[u]!=d) continue;
for(auto [v,c]:gr[u])
{
if(minimize(dist[v],d+c))
{
qq.emplace(dist[v],v);
}
}
}
}
struct info
{
ll d,u,s;
info(ll _d=0,ll _u=0,ll _s=0)
{
d=_d;
u=_u;
s=_s;
}
bool operator <(const info &o) const
{
return d>o.d;
}
};
ll dijkstra2(ll st,ll en)
{
memset(f,0x3f,sizeof f);
priority_queue<info> qq;
qq.emplace(0,st,0);
f[st][0]=0;
while(qq.size())
{
auto [d,u,s]=qq.top();
qq.pop();
if(d!=f[u][s]) continue;
for(auto [v,c]:gr[u])
{
if(c==0)
{
if(s==2) continue;
if(minimize(f[v][s|1],f[u][s]))
{
qq.emplace(f[v][s|1],v,s|1);
}
}
else
{
if(s==1)
{
if(minimize(f[v][2],f[u][s]+c))
{
qq.emplace(f[v][2],v,2);
}
}
else
{
if(minimize(f[v][s],f[u][s]+c))
{
qq.emplace(f[v][s],v,s);
}
}
}
}
}
return min(min(f[en][0],f[en][1]),f[en][2]);
}
int main()
{
online();
ll s,t,u,v;
cin>>n>>m;
cin>>s>>t;
cin>>u>>v;
for(int i=1;i<=m;i++)
{
ll u,v,c;
cin>>u>>v>>c;
U[i]=u;
V[i]=v;
C[i]=c;
gr[u].emplace_back(v,c);
gr[v].emplace_back(u,c);
}
dijkstra(s,dists);
dijkstra(t,distt);
for(int i=1;i<=m;i++)
{
if(dists[U[i]]+C[i]+distt[V[i]]==dists[t])
{
gr[U[i]].emplace_back(V[i],0);
}
}
cout<<min(dijkstra2(u,v),dijkstra2(v,u));
}
# | 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... |