이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
#define debug(x) cout << #x << " " << (x) << endl;
void g(vector<int> &a) {
for (auto el : a) cout << el << ", ";
cout << endl;
}
int n,m,s,t,u,v;
vector<vector<pair<int, int>>> adj;
vector<int> djikstra(int start) {
vector<int> dp(n, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, start});
// vector<int> vis(n);
while (!pq.empty()) {
auto [cost, node] = pq.top();pq.pop();
if (dp[node] == INF) {
dp[node] = cost;
for (auto [children, w] : adj[node]) pq.push({cost + w, children});
}
}
return dp;
}
int ans = INF;
#define info tuple<int,int,int>
vector<int> distU, distV;
void djikstra2(int start, int end) {
vector<vector<int>> dp(n, vector<int>(2, INF));
vector<int> dist(n, INF);
priority_queue<info, vector<info>, greater<info>> pq;
pq.push({0, start, -1});
while (!pq.empty()) {
auto [cost, node, par] = pq.top();pq.pop();
int par0 = par == -1 ? INF : dp[par][0];
int par1 = par == -1 ? INF : dp[par][1];
if (dist[node] == INF) {
dist[node] = cost;
dp[node][0] = min(distU[node], par0);
dp[node][1] = min(distV[node], par1);
for (auto [children, w] : adj[node]) {
pq.push({cost+w, children, node});
}
} else if (cost == dist[node]) {
if (min(distU[node], par0) + min(distV[node], par1) <= dp[node][0] + dp[node][1]) {
dp[node][0] = min(distU[node], par0);
dp[node][1] = min(distV[node], par1);
}
}
}
ans = min(ans, dp[end][0] + dp[end][1]);
}
void solve() {
cin>>n>>m>>s>>t>>u>>v;s--;t--;u--;v--;
adj = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>());
for (int i = 0; i < m; i++) {
int x,y,w;cin>>x>>y>>w;x--;y--;
adj[x].push_back({y, w});
adj[y].push_back({x, w});
}
distU = djikstra(u);
distV = djikstra(v);
// DAG from S
ans = distU[v];
djikstra2(s, t);
djikstra2(t, s);
cout << ans << endl;
}
int32_t main() {
solve();
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... |