이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define INF 1000000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define MOD 1000000007
using namespace std;
int n, m, s, t, u, v, sp;
vector<pair<int, int>> adj[100005];
vector<int> opte[100005];
bool visited[100005];
int dp[100005];
int ans;
void dij(int start, int arr[]){
for(int i = 1;i<=n;i++) arr[i] = INF;
arr[start] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, start});
while(!pq.empty()){
pair<int, int> curr = pq.top();
pq.pop();
if(curr.first != arr[curr.second]) continue;
for(pair<int, int> j: adj[curr.second]){
if((j.second + curr.first)>=arr[j.first]) continue;
arr[j.first] = j.second + curr.first;
pq.push({arr[j.first], j.first});
}
}
}
void dfs(int x){
if(visited[x]) return;
visited[x] = true;
for(int j: opte[x]){
dfs(j);
dp[x] = min(dp[x], dp[j]);
}
}
int findans(int a1[], int a2[]){
for(int i = 1;i<=n;i++){
visited[i] = false;
dp[i] = a2[i];
}
for(int i = 1;i<=n;i++){
dfs(i);
}
for(int i = 1;i<=n;i++){
ans = min(ans, dp[i] + a1[i]);
}
return ans;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>s>>t>>u>>v;
for(int i = 0;i<m;i++){
int a, b, c;
cin>>a>>b>>c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int sd[n+1], td[n+1], ud[n+1], vd[n+1];
dij(s, sd);
dij(t, td);
dij(u, ud);
dij(v, vd);
sp = sd[t];
ans = ud[v];
for(int i = 1;i<=n;i++){
for(pair<int, int> j: adj[i]){
if (sd[i] + j.second + td[j.first] == sp){
opte[i].push_back(j.first);
}
}
}
findans(ud, vd);
findans(vd, ud);
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... |