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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX_N = 1e5 + 10;
int N, M, S, T, U, V;
ll distS[MAX_N], distU[MAX_N], distV[MAX_N], minCostU[MAX_N], minCostV[MAX_N];
vector<pair<int, int>> adj[MAX_N];
bool visited[MAX_N];
void dijkstra(ll dist[], int s){
memset(dist, 0x3f, (N + 3) * sizeof(ll));
dist[s] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push(make_pair(0, s));
while(!pq.empty()){
int u = pq.top().se;
ll kc = pq.top().fi;
pq.pop();
if(kc > dist[u]) continue;
for(pair<int, int> o: adj[u]){
int v = o.fi;
ll newkc = kc + o.se;
if(minimize(dist[v], newkc)){
pq.push(make_pair(newkc, v));
}
}
}
}
void solve(){
cin >> N >> M >> S >> T >> U >> V;
for(int i = 1; i <= M; i++){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back(make_pair(v, c));
adj[v].push_back(make_pair(u, c));
}
dijkstra(distS, S);
dijkstra(distU, U);
dijkstra(distV, V);
memset(minCostU, 0x3f, sizeof minCostU);
memset(minCostV, 0x3f, sizeof minCostV);
priority_queue<pair<ll, int>> pq;
pq.push(make_pair(distS[T], T));
minCostU[T] = distU[T];
minCostV[T] = distV[T];
visited[T] = true;
ll ans = oo;
while(!pq.empty()){
int u = pq.top().se;
pq.pop();
minCostU[u] = distU[u];
minCostV[u] = distV[u];
for(pair<int, int> o: adj[u]){
int v = o.fi, kc = o.se;
if(!visited[v] && distS[u] == distS[v] + kc){
visited[v] = true;
pq.push(make_pair(distS[v], v));
}
if(distS[u] + kc == distS[v]){
minimize(minCostU[u], minCostU[v]);
minimize(minCostV[u], minCostV[v]);
}
}
minimize(ans, distU[u] + minCostV[u]);
minimize(ans, distV[u] + minCostU[u]);
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("capsomayman.inp","r",stdin);
// freopen("capsomayman.out","w",stdout);
int t = 1;
// cin >> t;
while(t--){
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... |