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 int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 5, INF = 1e15;
vector<array<int, 2> > g[N];
vector<int> distS, distT, distU, distV;
vector<array<int, 2> > between;
int n, m;
int s, t, u, v;
bool cmp(array<int, 2> a, array<int, 2> b) {
return a[0] < b[0];
}
void dijkstra(int src, vector<int> &dist) {
dist[src] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
pq.push(make_pair(dist[src], src));
while(!pq.empty()) {
int cur_dist = pq.top().first, node = pq.top().second;
pq.pop();
if(cur_dist != dist[node]) {
continue;
}
for(int i = 0; i < g[node].size(); i++) {
if(dist[g[node][i][0]] > dist[node] + g[node][i][1]) {
dist[g[node][i][0]] = dist[node] + g[node][i][1];
pq.push(make_pair(dist[g[node][i][0]], g[node][i][0]));
}
}
}
}
void Solve() {
cin >> n >> m >> s >> t >> u >> v;
for(int i = 1; i <= m; i++) {
int a, b, w;
cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
distS.resize(n + 1);
distT.resize(n + 1);
distU.resize(n + 1);
distV.resize(n + 1);
for(int i = 0; i <= n; i++) {
distS[i] = INF;
distT[i] = INF;
distU[i] = INF;
distV[i] = INF;
}
dijkstra(s, distS);
dijkstra(t, distT);
dijkstra(u, distU);
dijkstra(v, distV);
/*cout << "distance from S\n";
for(int i = 1; i <= n; i++) {
cout << distS[i] << " ";
}
cout << "\n";
cout << "distance from T\n";
for(int i = 1; i <= n; i++) {
cout << distT[i] << " ";
}
cout << "\n";
cout << "distance from U\n";
for(int i = 1; i <= n; i++) {
cout << distU[i] << " ";
}
cout << "\n";
cout << "distance from V\n";
for(int i = 1; i <= n; i++) {
cout << distV[i] << " ";
}
cout << "\n";*/
for(int i = 1; i <= n; i++) {
if(distS[i] + distT[i] == distS[t]) {
between.push_back({distS[i], i});
}
}
sort(between.begin(), between.end(), cmp);
vector<array<int, 2> > dp(n + 1);
for(int i = 0; i <= n; i++) {
dp[i] = {INF, INF};
}
int curAns = distU[v];
for(int i = 0; i < between.size(); i++) {
int node = between[i][1];
//cout << "node = " << node << "\n";
dp[node][0] = min(dp[node][0], distU[node]);
dp[node][1] = min(dp[node][1], distV[node]);
for(int j = 0; j < g[node].size(); j++) {
dp[g[node][j][0]][0] = min(dp[g[node][j][0]][0], dp[node][0]);
dp[g[node][j][0]][1] = min(dp[g[node][j][0]][1], dp[node][1]);
}
curAns = min(curAns, dp[node][0] + distV[node]);
curAns = min(curAns, dp[node][1] + distU[node]);
}
cout << curAns << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstra(long long int, std::vector<long long int>&)':
commuter_pass.cpp:28:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'void Solve()':
commuter_pass.cpp:95:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 0; i < between.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
commuter_pass.cpp:100:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j = 0; j < g[node].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |