This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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){
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();
}
}
}
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();
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(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';
// }
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... |