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;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef MY_DEBUG_FLAG
template<typename T> void debug(T a) { cerr << a << ' '; }
template<typename T, typename U> void debug(pair<T, U> a) { cerr << a.first << ' ' << a.second << ' '; }
template<typename T> void debug(vector<T> a) { for(auto it : a) debug(it);}
template<typename T> void debug(set<T> a) { for(auto it : a) debug(it);}
#define db(a) cerr << "DEBUG ( " << #a << " == "; debug(a); cerr << ")\n"
#else
#define db(...)
#endif
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr)
#define sz(a) ((int)(a).size())
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define dec(i,n,a) for(int i=(n); i>=(a); i--)
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()
constexpr ll inf = 0x3f3f3f3f3f3f3f3f;
constexpr int MAXN = 1e5 + 10;
constexpr int mod = 1000000007;
struct Edge
{
int u, v, w;
} edges[MAXN];
vector<pii> g[MAXN];
ll dist[MAXN][4]; // 0 -> s, 1 -> t, 2 -> u, 3 -> v
void dijkstra(int s, int k) {
rep(i,0,MAXN) dist[i][k] = inf;
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> q;
q.push({0, s});
bool mark[MAXN]; clr(mark, 0);
while(!q.empty()) {
int u = q.top().ss;
ll d = q.top().ff;
q.pop();
if(mark[u]) continue;
mark[u] = 1;
dist[u][k] = d;
for(auto nxt : g[u]) {
int v = nxt.ff, w = nxt.ss;
if(dist[v][k] < d + w || mark[v]) continue;
q.push({d + w, v});
}
}
}
vi dag[MAXN], inv_dag[MAXN], poss;
int grau[MAXN];
int main() {
int n, m; scanf("%d %d", &n, &m);
int s, t; scanf("%d %d", &s, &t);
int u, v; scanf("%d %d", &u, &v);
rep(i,0,m) {
int a, b, w; scanf("%d %d %d", &a, &b, &w);
edges[i] = {a,b,w};
g[a].pb({b,w});
g[b].pb({a,w});
}
dijkstra(s,0);
dijkstra(t,1);
dijkstra(u,2);
dijkstra(v,3);
ll menor = dist[t][0];
rep(i,0,m) {
int x = edges[i].u, y = edges[i].v, w = edges[i].w;
if(dist[x][0] > dist[y][0]) swap(x,y);
if(dist[x][0] + w + dist[y][1] == menor)
dag[x].pb(y), inv_dag[y].pb(x), grau[y]++, poss.pb(x);
}
vi ord;
queue<int> q;
for(auto x : poss)
if(!grau[x]) q.push(x);
while(!q.empty()) {
int x = q.front();
ord.pb(x);
q.pop();
for(auto y : dag[x]){
grau[y]--;
if(!grau[y]) q.push(y);
}
}
ll d[MAXN]; clr(d,0);
rep(i,1,n+1) d[i] = dist[i][2];
for(auto x : ord) {
for(auto y : dag[x]) {
dist[y][2] = min(dist[x][2], dist[y][2]);
}
}
reverse(all(ord));
for(auto x : ord) {
for(auto y : inv_dag[x]) {
d[y] = min(d[x], d[y]);
}
}
rep(i,1,n+1) {
dist[i][2] = min(dist[i][2], d[i]);
}
ll ans = inf;
rep(i,0,n) {
ans = min(ans, dist[i][2] + dist[i][3]);
}
printf("%lld\n", ans);
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:71:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m; scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
commuter_pass.cpp:72:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int s, t; scanf("%d %d", &s, &t);
~~~~~^~~~~~~~~~~~~~~~~
commuter_pass.cpp:73:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v; scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
commuter_pass.cpp:75:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b, w; scanf("%d %d %d", &a, &b, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |