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 X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 1e5 + 7;
vector<pii> v[N];
ll d[4][N];
int n, m, s, t, x, y;
void dijkstra(ll d[], int s) {
FOR(i, 1, n) d[i] = 1e18;
priority_queue<pli, vector<pli>, greater<pli>> pq;
d[s] = 0;
pq.push({d[s], s});
while (!pq.empty()) {
pli tmp = pq.top();
pq.pop();
ll w = tmp.X;
int u = tmp.Y;
if (w != d[u]) continue;
for (auto x : v[u])
if (minimize(d[x.X], w + x.Y)) pq.push({d[x.X], x.X});
}
}
ll f[N], g[N];
int deg[N];
vector<int> adj[N];
bool onRoute[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>s>>t>>x>>y;
FOR(i, 1, m) {
int x, y, c;
cin>>x>>y>>c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
dijkstra(d[0], s);
dijkstra(d[1], t);
dijkstra(d[2], x);
dijkstra(d[3], y);
FOR(i, 1, n)
if (d[0][i] + d[1][i] == d[0][t]) onRoute[i] = true;
FOR(x, 1, n)
for (auto y : v[x])
if (d[0][x] + y.Y == d[0][y.X]) {
adj[x].push_back(y.X);
deg[y.X]++;
}
//// Topo
vector<int> T;
queue<int> qu;
FOR(i, 1, n)
if (deg[i] == 0) qu.push(i);
while (!qu.empty()) {
int x = qu.front();
qu.pop();
T.push_back(x);
for (auto y : adj[x])
if (--deg[y] == 0) qu.push(y);
}
reverse(all(T));
for (auto x : T) {
f[x] = g[x] = 1e18;
if (onRoute[x]) f[x] = d[2][x], g[x] = d[3][x];
for (auto y : adj[x]) {
minimize(f[x], f[y]);
minimize(g[x], g[y]);
}
}
ll res = d[2][y];
FOR(i, 1, n)
if (onRoute[i]) {
minimize(res, d[2][i] + g[i]);
minimize(res, d[3][i] + f[i]);
}
cout<<res;
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... |