이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
int S,T;cin>>S>>T;
--S;--T;
int U,V;cin>>U>>V;
--U;--V;
vector<vector<pair<int,long long>>>adj(n);
for (int i = 0;i<m;++i){
int x,y,z;cin>>x>>y>>z;
--x;--y;
adj[x].push_back({y,z});
adj[y].push_back({x,z});
}
const long long mxn = 1e15;
priority_queue<pair<long long,pair<int,int>>>q;
vector<vector<long long>>dist(n,vector<long long>(2,mxn));
q.push({0,{U,0}});
q.push({0,{V,1}});
dist[U][0] = 0;
dist[V][1] = 0;
while(!q.empty()){
auto u = q.top().second;
q.pop();
for (auto x:adj[u.first]){
if (dist[x.first][u.second] > dist[u.first][u.second] + x.second){
dist[x.first][u.second] = dist[u.first][u.second] + x.second;
q.push({-dist[x.first][u.second],{x.first,u.second}});
}
}
}
vector<int>visited(n,0);
long long ans = dist[U][1];
vector<long long>dis(n,mxn);
auto bfs = [&](){
priority_queue<pair<long long,int>>qq;
qq.push({0,S});
dis[S] = 0;
while(!qq.empty()){
auto u = qq.top().second;
qq.pop();
for (auto x:adj[u]){
if (dis[x.first] > dis[u] + x.second){
dis[x.first] = dis[u] + x.second;
qq.push({-dis[x.first],x.first});
}
}
}
};
vector<pair<long long,long long>>dp(n,{mxn,mxn});
bfs();
function<pair<long long,long long>(int,long long,long long,long long)>dfs = [&](int u,long long dd,long long pp,long long bb){
if (u == T){
return make_pair(pp,bb);
}
if (dp[u]!=make_pair(mxn,mxn))return dp[u];
for (auto x:adj[u]){
if (dd + x.second <=dis[x.first]){
pair<long long,long long>v = dfs(x.first,dd + x.second,min(pp,dist[x.first][0]),min(dist[x.first][1],bb));
if (v.first + v.second < dp[u].first + dp[u].second){
dp[u] = v;
}
}
}
return dp[u];
};
auto x = dfs(S,0,dist[S][0],dist[S][1]);
ans = min(ans,x.first + x.second);
cout<<ans<<'\n';
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... |