이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n, m, s, t, u, v, ans = 1e18, dis[100001];
vector<vector<pair<ll, ll>>> G(100001);
vector<int> E[100001], par[100001];
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
bool vis[100001];
void reset() { for(int i = 1; i <= n; i++) dis[i] = LLONG_MAX; }
void cal(){
reset();
/*pq.push({0, u});
dis[u] = 0;
while(!pq.empty()){
ll now = pq.top().second;
pq.pop();
if(now == v) break;
for(auto &[e, w] : G[now]){
if(dis[e] > dis[now] + w){
dis[e] = dis[now] + w;
pq.push({dis[e], e});
}
}
}*/
ans = min(ans, dis[v]);
}
void solve(int d){
int tmp = d;
while(d != s && par[d].size() == 1){
int e = par[d][0];
G[d].push_back({e, 0});
G[e].push_back({d, 0});
d = e;
}
if(d == s) {
cal();
}else {
for(auto &e : par[d]){
G[d].push_back({e, 0});
G[e].push_back({d, 0});
solve(e);
G[d].pop_back();
G[e].pop_back();
}
}
d = tmp;
while(d != s && par[d].size() == 1){
int e = par[d][0];
G[d].pop_back();
G[e].pop_back();
d = e;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> t >> u >> v;
for(ll i = 0; i < m; i++){
ll u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
reset();
pq.push({0, s});
dis[s] = 0;
while(!pq.empty()){
ll now = pq.top().second;
pq.pop();
if(now == t) break;
for(auto &[e, w] : G[now]){
if(dis[e] > dis[now] + w){
dis[e] = dis[now] + w;
pq.push({dis[e], e});
}
}
}
pq.push({0, s});
while(!pq.empty()){
ll now = pq.top().second;
pq.pop();
if(now == t) break;
if(vis[now]) continue;
vis[now] = 1;
for(auto &[e, w] : G[now]){
if(dis[e] == dis[now] + w){
par[e].push_back(now);
pq.push({dis[e], e});
}
}
}
// for(int i = 1; i <= n; i++){
// cout << "i: " << i << '\n';
// for(auto &e : par[i]) cout << e << ' ';
// cout << '\n';
// }
reset();
solve(t);
cout << ans;
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... |