이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
const ll MAXN = 1e5 + 5;
const ll INF = 1ll<<62;
ll N,M,S,T,U,V,D[MAXN],vis[MAXN];
vector<pi> edge[MAXN];
vector<ll> succ[MAXN], pred[MAXN];
void dfs(int i){
vis[i] = 1;
if(i == S) return;
for(auto e : edge[i]){
if(D[e.first] + e.second == D[i]){
succ[e.first].push_back(i);
pred[i].push_back(e.first);
if(!vis[e.first]) dfs(e.first);
}
}
}
int main(){
cin >> N >> M >> S >> T >> U >> V;
for(int i = 0; i < M; i++){
ll ai, bi, ci;
cin >> ai >> bi >> ci;
edge[bi].push_back({ai, ci});
edge[ai].push_back({bi, ci});
}
priority_queue<pi, vector<pi>, greater<pi>> pq;
fill(begin(D), end(D), INF);
D[S] = 0;
pq.push({0, S});
while(!pq.empty()){
pi t = pq.top(); pq.pop();
if(t.first != D[t.second]) continue;
if(t.second == T) break;
for(auto e : edge[t.second]){
if(D[t.second] + e.second < D[e.first]){
pq.push({D[e.first] = D[t.second] + e.second, e.first});
}
}
}
dfs(T);
pq = priority_queue<pi, vector<pi>, greater<pi>>();
fill(begin(D), end(D), INF);
fill(begin(vis), end(vis), 0);
vis[U] = 1;
D[U] = 0;
pq.push({0, U});
while(!pq.empty()){
pi t = pq.top(); pq.pop();
if(t.first != D[t.second]) continue;
if(t.second == V) break;
vis[t.second] = 1;
for(int e : succ[t.second]){
if(!vis[e]){
vis[e] = 1;
pq.push({D[e] = D[t.second], e});
}
}
for(auto e : edge[t.second]){
if(D[t.second] + e.second < D[e.first] && !vis[e.first]){
pq.push({D[e.first] = D[t.second] + e.second, e.first});
}
}
}
ll ans = D[V];
//now do it again with pred
pq = priority_queue<pi, vector<pi>, greater<pi>>();
fill(begin(D), end(D), INF);
fill(begin(vis), end(vis), 0);
vis[U] = 1;
D[U] = 0;
pq.push({0, U});
while(!pq.empty()){
pi t = pq.top(); pq.pop();
if(t.first != D[t.second]) continue;
if(t.second == V) break;
vis[t.second] = 1;
for(int e : pred[t.second]){
if(!vis[e]){
vis[e] = 1;
pq.push({D[e] = D[t.second], e});
}
}
for(auto e : edge[t.second]){
if(D[t.second] + e.second < D[e.first] && !vis[e.first]){
pq.push({D[e.first] = D[t.second] + e.second, e.first});
}
}
}
cout << min(ans, D[V]) << "\n";
}
# | 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... |