#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
const int PRIME = 1e9 + 7;
const int MX = 100000;
int n, m, s, t, u, v;
vector<pair<int, int>> adjList[MX];
ll distFromU[MX], distFromV[MX], dpU[MX], dpV[MX], dist[MX];
void doDijkstra(int source, ll arr[]){
// vector<ll> dist(n, LLONG_MAX);
fill(arr, arr + MX, LLONG_MAX / 3);
arr[source] = 0;
using DistNode = pair<ll, int>;
priority_queue<DistNode, vector<DistNode>, greater<DistNode>> pq;
pq.push({0, source});
while(!pq.empty()){
int currNode = pq.top().second;
if(arr[currNode] < pq.top().first){
pq.pop();
continue;
}
pq.pop();
for(auto edge: adjList[currNode]){
if(arr[currNode] + edge.second < arr[edge.first]){
pq.push({arr[edge.first] = arr[currNode] + edge.second, edge.first});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
s--; t--; u--; v--;
for(int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
a--; b--;
adjList[a].push_back({b, c});
adjList[b].push_back({a, c});
}
doDijkstra(u, distFromU);
doDijkstra(v, distFromV);
// vector<ll> distFromU = doDijkstra(u);
// vector<ll> distFromV = doDijkstra(v);
// vector<ll> dpU(n, LLONG_MAX / 3);
// vector<ll> dpV(n, LLONG_MAX / 3);
fill(dpU, dpU + MX, LLONG_MAX / 3);
fill(dpV, dpV + MX, LLONG_MAX / 3);
fill(dist, dist + MX, LLONG_MAX / 3);
// vector<ll> dist(n, LLONG_MAX / 3);
dist[s] = 0;
dpU[s] = distFromU[s];
dpV[s] = distFromV[s];
using DistNodeSrc = pair<ll, pair<int, int>>;
priority_queue<DistNodeSrc, vector<DistNodeSrc>, greater<DistNodeSrc>> pq;
pq.push({0, {s, s}});
while(!pq.empty()){
int currNode = pq.top().second.first;
int parent = pq.top().second.second;
if(dist[currNode] < pq.top().first){
pq.pop();
continue;
}
pq.pop();
// cout << currNode + 1 << "CURR " << parent + 1 << '\n';
// cout << distFromU[currNode] << "dfU " << dpU[parent] << '\n';
// cout << distFromV[currNode] << "dfV " << dpV[parent] << '\n';
// cout << endl;
if(min(distFromU[currNode], dpU[parent]) + min(distFromV[currNode], dpV[parent]) <=
dpU[currNode] + dpV[currNode]){
dpU[currNode] = min(distFromU[currNode], dpU[parent]);
dpV[currNode] = min(distFromV[currNode], dpV[parent]);
}
// if(dist[currNode] == pq.top().first){
// if(min(distFromU[currNode], dpU[parent]) + min(distFromV[currNode], dpV[parent]) <= dpU[currNode] + dpV[currNode])
// {
// dpU[currNode] = min(distFromU[currNode], dpU[parent]);
// dpV[currNode] = min(distFromV[currNode], dpV[parent]);
// }
// }else{
// dpU[currNode] = min(distFromU[currNode], dpU[parent]);
// dpV[currNode] = min(distFromV[currNode], dpV[parent]);
// }
for(auto edge: adjList[currNode]){
if(dist[currNode] + edge.second < dist[edge.first]){
// cout << "WUB";
dpU[edge.first] = LLONG_MAX / 3;
dpV[edge.first] = LLONG_MAX / 3;
pq.push({dist[edge.first] = dist[currNode] + edge.second, {edge.first, currNode}});
}else if(dist[currNode] + edge.second == dist[edge.first]){
if(min(distFromU[edge.first], dpU[currNode]) + min(distFromV[edge.first], dpV[currNode]) <=
dpU[edge.first] + dpV[edge.first]){
dpU[edge.first] = min(distFromU[edge.first], dpU[currNode]);
dpV[edge.first] = min(distFromV[edge.first], dpV[currNode]);
}
}
}
}
ll ans = min(distFromU[v], dpU[t] + dpV[t]);
fill(dpU, dpU + MX, LLONG_MAX / 3);
fill(dpV, dpV + MX, LLONG_MAX / 3);
fill(dist, dist + MX, LLONG_MAX / 3);
// for(int i = 0; i < n; i++){
// cerr << dpU[i] << ' ' << dpV[i] << '\n';
// }
dist[t] = 0;
dpU[t] = distFromU[t];
dpV[t] = distFromV[t];
pq.push({0, {t, t}});
while(!pq.empty()){
int currNode = pq.top().second.first;
int parent = pq.top().second.second;
if(dist[currNode] < pq.top().first){
pq.pop();
continue;
}
pq.pop();
if(min(distFromU[currNode], dpU[parent]) + min(distFromV[currNode], dpV[parent]) <=
dpU[currNode] + dpV[currNode]){
dpU[currNode] = min(distFromU[currNode], dpU[parent]);
dpV[currNode] = min(distFromV[currNode], dpV[parent]);
}
for(auto edge: adjList[currNode]){
if(dist[currNode] + edge.second < dist[edge.first]){
// cout << "WUB";
dpU[edge.first] = LLONG_MAX / 3;
dpV[edge.first] = LLONG_MAX / 3;
pq.push({dist[edge.first] = dist[currNode] + edge.second, {edge.first, currNode}});
}else if(dist[currNode] + edge.second == dist[edge.first]){
if(min(distFromU[edge.first], dpU[currNode]) + min(distFromV[edge.first], dpV[currNode]) <=
dpU[edge.first] + dpV[edge.first]){
dpU[edge.first] = min(distFromU[edge.first], dpU[currNode]);
dpV[edge.first] = min(distFromV[edge.first], dpV[currNode]);
}
}
}
}
// ll ans = distFromU[v];
// for(int i = 0; i < n; i++){
// if(distS[i] + distFromT[i] == distS[t]){
// ans = min(ans, dp[i]);
// }
// // cout << el << '\n';
// }
cout << min(ans, dpU[s] + dpV[s]) << '\n';
// cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
374 ms |
16008 KB |
Output is correct |
2 |
Correct |
381 ms |
14880 KB |
Output is correct |
3 |
Correct |
360 ms |
15952 KB |
Output is correct |
4 |
Correct |
349 ms |
14828 KB |
Output is correct |
5 |
Correct |
356 ms |
14792 KB |
Output is correct |
6 |
Correct |
382 ms |
16076 KB |
Output is correct |
7 |
Correct |
369 ms |
14996 KB |
Output is correct |
8 |
Correct |
373 ms |
14796 KB |
Output is correct |
9 |
Correct |
373 ms |
14696 KB |
Output is correct |
10 |
Correct |
332 ms |
14556 KB |
Output is correct |
11 |
Correct |
166 ms |
9708 KB |
Output is correct |
12 |
Correct |
374 ms |
14708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
14928 KB |
Output is correct |
2 |
Correct |
413 ms |
14984 KB |
Output is correct |
3 |
Correct |
387 ms |
14976 KB |
Output is correct |
4 |
Correct |
408 ms |
14812 KB |
Output is correct |
5 |
Correct |
388 ms |
14752 KB |
Output is correct |
6 |
Correct |
380 ms |
14828 KB |
Output is correct |
7 |
Correct |
377 ms |
14764 KB |
Output is correct |
8 |
Correct |
383 ms |
14824 KB |
Output is correct |
9 |
Correct |
391 ms |
14728 KB |
Output is correct |
10 |
Correct |
391 ms |
14888 KB |
Output is correct |
11 |
Correct |
155 ms |
9708 KB |
Output is correct |
12 |
Correct |
383 ms |
15016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7276 KB |
Output is correct |
2 |
Correct |
5 ms |
6636 KB |
Output is correct |
3 |
Correct |
5 ms |
6636 KB |
Output is correct |
4 |
Correct |
20 ms |
8684 KB |
Output is correct |
5 |
Correct |
13 ms |
7660 KB |
Output is correct |
6 |
Correct |
6 ms |
6636 KB |
Output is correct |
7 |
Correct |
6 ms |
6764 KB |
Output is correct |
8 |
Correct |
6 ms |
6764 KB |
Output is correct |
9 |
Correct |
5 ms |
6636 KB |
Output is correct |
10 |
Correct |
13 ms |
7660 KB |
Output is correct |
11 |
Correct |
5 ms |
6636 KB |
Output is correct |
12 |
Correct |
5 ms |
6636 KB |
Output is correct |
13 |
Correct |
5 ms |
6636 KB |
Output is correct |
14 |
Correct |
6 ms |
6636 KB |
Output is correct |
15 |
Correct |
7 ms |
6636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
374 ms |
16008 KB |
Output is correct |
2 |
Correct |
381 ms |
14880 KB |
Output is correct |
3 |
Correct |
360 ms |
15952 KB |
Output is correct |
4 |
Correct |
349 ms |
14828 KB |
Output is correct |
5 |
Correct |
356 ms |
14792 KB |
Output is correct |
6 |
Correct |
382 ms |
16076 KB |
Output is correct |
7 |
Correct |
369 ms |
14996 KB |
Output is correct |
8 |
Correct |
373 ms |
14796 KB |
Output is correct |
9 |
Correct |
373 ms |
14696 KB |
Output is correct |
10 |
Correct |
332 ms |
14556 KB |
Output is correct |
11 |
Correct |
166 ms |
9708 KB |
Output is correct |
12 |
Correct |
374 ms |
14708 KB |
Output is correct |
13 |
Correct |
376 ms |
14928 KB |
Output is correct |
14 |
Correct |
413 ms |
14984 KB |
Output is correct |
15 |
Correct |
387 ms |
14976 KB |
Output is correct |
16 |
Correct |
408 ms |
14812 KB |
Output is correct |
17 |
Correct |
388 ms |
14752 KB |
Output is correct |
18 |
Correct |
380 ms |
14828 KB |
Output is correct |
19 |
Correct |
377 ms |
14764 KB |
Output is correct |
20 |
Correct |
383 ms |
14824 KB |
Output is correct |
21 |
Correct |
391 ms |
14728 KB |
Output is correct |
22 |
Correct |
391 ms |
14888 KB |
Output is correct |
23 |
Correct |
155 ms |
9708 KB |
Output is correct |
24 |
Correct |
383 ms |
15016 KB |
Output is correct |
25 |
Correct |
13 ms |
7276 KB |
Output is correct |
26 |
Correct |
5 ms |
6636 KB |
Output is correct |
27 |
Correct |
5 ms |
6636 KB |
Output is correct |
28 |
Correct |
20 ms |
8684 KB |
Output is correct |
29 |
Correct |
13 ms |
7660 KB |
Output is correct |
30 |
Correct |
6 ms |
6636 KB |
Output is correct |
31 |
Correct |
6 ms |
6764 KB |
Output is correct |
32 |
Correct |
6 ms |
6764 KB |
Output is correct |
33 |
Correct |
5 ms |
6636 KB |
Output is correct |
34 |
Correct |
13 ms |
7660 KB |
Output is correct |
35 |
Correct |
5 ms |
6636 KB |
Output is correct |
36 |
Correct |
5 ms |
6636 KB |
Output is correct |
37 |
Correct |
5 ms |
6636 KB |
Output is correct |
38 |
Correct |
6 ms |
6636 KB |
Output is correct |
39 |
Correct |
7 ms |
6636 KB |
Output is correct |
40 |
Correct |
363 ms |
20264 KB |
Output is correct |
41 |
Correct |
372 ms |
18948 KB |
Output is correct |
42 |
Correct |
391 ms |
19052 KB |
Output is correct |
43 |
Correct |
163 ms |
11884 KB |
Output is correct |
44 |
Correct |
193 ms |
11884 KB |
Output is correct |
45 |
Correct |
334 ms |
17988 KB |
Output is correct |
46 |
Correct |
311 ms |
17600 KB |
Output is correct |
47 |
Correct |
376 ms |
19792 KB |
Output is correct |
48 |
Correct |
157 ms |
11348 KB |
Output is correct |
49 |
Correct |
316 ms |
19872 KB |
Output is correct |
50 |
Correct |
329 ms |
18032 KB |
Output is correct |
51 |
Correct |
310 ms |
17996 KB |
Output is correct |