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;
int n, m;
int u, v, s, t;
vector<vector<pair<int, int> > > adj;
vector<vector<int> > adj2;
vector<long long> udis, vdis, dis, mini1, mini2;
priority_queue<pair<long long, int> > pq;
vector<int> pre;
vector<bool> vis;
stack<int> topo;
void dfs(int index) {
if (vis[index]) return;
vis[index] = true;
for (int i = 0; i < adj[index].size(); i++) {
int x = adj[index][i].first;
int d = adj[index][i].second;
if (dis[index] - d == dis[x]) {
adj2[index].push_back(x);
dfs(x);
}
}
topo.push(index);
}
int main() {
scanf("%d %d %d %d %d %d", &n, &m, &s, &t, &u, &v);
s--;t--;u--;v--;
int i1, i2, i3;
adj.resize(n); adj2.resize(n);
udis.resize(n, 101234567890123456LL);
vdis.resize(n, 101234567890123456LL);
dis.resize(n, 101234567890123456LL);
mini1.resize(n, 101234567890123456LL);
mini2.resize(n, 101234567890123456LL);
vis.resize(n, false);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &i1, &i2, &i3);
i1--; i2--;
adj[i1].emplace_back(i2, i3);
adj[i2].emplace_back(i1, i3);
}
udis[u] = 0;
pq.push(make_pair(0, u));
while (!pq.empty()) {
int x = pq.top().second;
long long d = -pq.top().first;
pq.pop();
if (udis[x] != d) continue;
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i].first;
int w = adj[x][i].second;
if (udis[y] > d + w) {
udis[y] = d + w;
pq.push(make_pair(-d-w, y));
}
}
}
vdis[v] = 0;
pq.push(make_pair(0, v));
while (!pq.empty()) {
int x = pq.top().second;
long long d = -pq.top().first;
pq.pop();
if (vdis[x] != d) continue;
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i].first;
int w = adj[x][i].second;
if (vdis[y] > d + w) {
vdis[y] = d + w;
pq.push(make_pair(-d-w, y));
}
}
}
dis[s] = 0;
pq.push(make_pair(0, s));
while (!pq.empty()) {
int x = pq.top().second;
long long d = -pq.top().first;
pq.pop();
if (dis[x] != d) continue;
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i].first;
int w = adj[x][i].second;
if (dis[y] > d + w) {
dis[y] = d + w;
pq.push(make_pair(-d-w, y));
}
}
}
dfs(t);
long long ans = udis[v];
while (!topo.empty()) {
int x = topo.top();
topo.pop();
mini1[x] = min(mini1[x], udis[x]);
mini2[x] = min(mini2[x], vdis[x]);
ans = min(ans, vdis[x] + mini1[x]);
ans = min(ans, udis[x] + mini2[x]);
for (int i = 0; i < adj2[x].size(); i++) {
int y = adj2[x][i];
mini1[y] = min(mini1[y], mini1[x]);
mini2[y] = min(mini2[y], mini2[x]);
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dfs(int)':
commuter_pass.cpp:17:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[index].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:52:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[x].size(); i++) {
~~^~~~~~~~~~~~~~~
commuter_pass.cpp:68:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[x].size(); i++) {
~~^~~~~~~~~~~~~~~
commuter_pass.cpp:84:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[x].size(); i++) {
~~^~~~~~~~~~~~~~~
commuter_pass.cpp:102:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj2[x].size(); i++) {
~~^~~~~~~~~~~~~~~~
commuter_pass.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d %d %d", &n, &m, &s, &t, &u, &v);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &i1, &i2, &i3);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |