이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=1e5+9;
const long long inf=2e15;
vector<pil>a[maxn];
int n,m;
struct cus{
int u;
long long w;
bool operator< (const cus &p)const {
return w>p.w;
}
};
vector<long long> sp(int u1){
vector<long long>dis;
dis.resize(n+1);
for1(i,1,n)dis[i]=inf;
dis[u1]=0;
vector<bool>visit;
visit.resize(n+1);
priority_queue<cus>c;
c.push({u1,0});
while (!c.empty()){
auto u=c.top();
c.pop();
if (visit[u.u])continue;
visit[u.u]=1;
for (auto v:a[u.u]){
long long newval=v.se+u.w;
if (newval<dis[v.fi]){
dis[v.fi]=newval;
c.push({v.fi,newval});
}
}
}
return dis;
}
vector<long long>disu,disv;
long long f[maxn][2][2];
int s,t,u1,v1;
long long solve(){
vector<long long>dis;
dis.resize(n+1);
for1(i,1,n)dis[i]=inf;
for1(i,1,n){
for1(j,0,1){
for1(k,0,1){
f[i][j][k]=inf;
}
}
f[i][1][0]=disu[i];
f[i][1][1]=disu[i]+disv[i];
f[i][0][1]=disv[i];
}
dis[s]=0;
vector<bool>visit;
visit.resize(n+1);
priority_queue<cus>c;
c.push({s,0});
while (!c.empty()){
auto u=c.top();
c.pop();
//if (u.u==2)cout<<u.w<<'\n';
//cout<<u.u<<'\n';
if (visit[u.u])continue;
visit[u.u]=1;
f[u.u][1][0]=min(f[u.u][1][0],disu[u.u]);
f[u.u][0][1]=min(f[u.u][0][1],disv[u.u]);
f[u.u][1][1]=min({f[u.u][1][1],f[u.u][1][0]+disv[u.u],f[u.u][0][1]+disu[u.u]});
//if (u.u==2)cout<<u.w<<'\n';
for (auto v:a[u.u]){
long long newval=v.se+u.w;
if (newval<dis[v.fi]){
dis[v.fi]=newval;
c.push({v.fi,newval});
f[v.fi][1][0]=f[u.u][1][0];
f[v.fi][1][1]=f[u.u][1][1];
f[v.fi][0][1]=f[u.u][0][1];
}
else if (newval==dis[v.fi]){
f[v.fi][1][0]=min(f[v.fi][1][0],f[u.u][1][0]);
f[v.fi][1][1]=min(f[v.fi][1][1],f[u.u][1][1]);
f[v.fi][0][1]=min(f[v.fi][0][1],f[u.u][0][1]);
}
}
}
return f[t][1][1];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>m;
cin>>s>>t>>u1>>v1;
for1(i,1,m){
int u,v;
long long w;
cin>>u>>v>>w;
a[u].pb({v,w});
a[v].pb({u,w});
}
disu=sp(u1),disv=sp(v1);
cout<<min(solve(),disu[v1]);
//solve();
//cout<<f[1][0][1];
}
# | 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... |