이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,int>
using namespace std;
const int maxn = 1e5 + 5;
ll dist[maxn][2],dp[maxn],D[maxn][2];
bool mark[maxn];
vector <vector<pir>> vec(maxn);
void input(int n,int m){
while (m--){
int u,v,w;
cin >> u >> v >> w;
vec[u].push_back({v,w});
vec[v].push_back({u,w});
}
}
void dijkstra(int source,int n,int st){
for (int i = 1 ; i <= n ; i++) mark[i] = 0;
priority_queue <pirll,vector<pirll>,greater<pirll>> pq;
pq.push({0,source});
while (pq.size()){
pirll t = pq.top();
pq.pop();
int u = t.se;ll w = t.fi;
if (mark[u]) continue;
mark[u] = 1;
dist[u][st] = w;
for (pir v : vec[u])
if (!mark[v.fi])
pq.push({v.se + w,v.fi});
}
}
vector <int> traverse_order;
void sol(int source,int n,int st){
for (int i = 1 ; i <= n ; i++) mark[i] = 0;
priority_queue <pirll,vector<pirll>,greater<pirll>> pq;
pq.push({0,source});
while (pq.size()){
pirll t = pq.top();
pq.pop();
int u = t.se;ll w = t.fi;
if (mark[u]) continue;
mark[u] = 1;
D[u][st] = w;
if (!st)
traverse_order.push_back(u);
for (pir v : vec[u])
if (!mark[v.fi])
pq.push({v.se + w,v.fi});
}
}
ll solve(int n,int T,int U,int V){
ll inf = 1e18;
for (int i = 1 ; i <= n ; i++) dp[i] = inf;
ll res = dist[V][0];
for (int u : traverse_order)
if (D[u][0] + D[u][1] == D[T][0]){
dp[u] = min(dp[u],dist[u][0]);
ll tmp = inf;
for (pir node : vec[u]){
int v = node.fi,w = node.se;
if (D[v][0] + w == D[u][0]){
tmp = min(tmp,dp[v] + dist[u][1]);
dp[u] = min(dp[u],dp[v]);
}
}
res = min(res,tmp);
}
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m,S,T,U,V;
cin >> n >> m >> S >> T >> U >> V;
input(n,m);
dijkstra(U,n,0);
dijkstra(V,n,1);
sol(S,n,0);
sol(T,n,1);
cout << solve(n,T,U,V);
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... |