// Om Namah Shivaya
// International Master in 40 days
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
bool iseven(ll a) {
if (!(a & 1)) return true;
else return false;
}
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#define debug(x) cout << #x << " = "; print(x); cout << endl
void print(int i) {
cout << i;
}
void print(ll i) {
cout << i;
}
void print(string i) {
cout << i;
}
void print(char i) {
cout << i;
}
void print(double i) {
cout << i;
}
void print(ld i) {
cout << i;
}
template<typename T, typename V>
void print(pair<T, V> v) {
cout << "{";
print(v.ff);
cout << ",";
print(v.ss);
cout << "}";
}
template<typename T>
void print(vector<T> v) {
cout << "[ ";
for (auto i : v) {
print(i);
cout << " ";
}
cout << "]";
}
template<typename T>
void print(set<T> v) {
cout << "[ ";
for (auto i : v) {
print(i);
cout << " ";
}
cout << "]";
}
template<typename T>
void print(multiset<T> v) {
cout << "[ ";
for (auto i : v) {
print(i);
cout << " ";
}
cout << "]";
}
template<typename T>
void print(Tree<T> v) {
cout << "[ ";
for (auto i : v) {
print(i);
cout << " ";
}
cout << "]";
}
template<typename T, typename V>
void print(map<T, V> v) {
cout << "{ ";
for (auto p : v) {
print(p);
cout << " ";
}
cout << "}";
}
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<pll> adj[maxn];
vector<ll> adj2[maxn];
vector<bool> vis(maxn);
vector<ll> topo;
void dfs(ll node) {
vis[node] = 1;
trav(child, adj2[node]) {
if (vis[child]) conts;
dfs(child);
}
topo.pb(node);
}
void solve(int test_case)
{
// https://usaco.guide/problems/joi-2018commuter-pass/solution
ll n, m; cin >> n >> m;
ll s, t; cin >> s >> t;
ll x, y; cin >> x >> y;
rep1(i, m) {
ll u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
auto dijkstra = [&](ll src, vector<ll> &dis) {
priority_queue< pll, vector<pll>, greater<pll> > pq;
fill(all(vis), 0);
pq.push({0, src});
dis[src] = 0;
while (!pq.empty()) {
auto [cost, node] = pq.top();
pq.pop();
if (vis[node]) conts;
vis[node] = 1;
for (auto [child, w] : adj[node]) {
ll cost2 = cost + w;
if (cost2 < dis[child]) {
dis[child] = cost2;
pq.push({cost2, child});
}
}
}
};
auto get = [&]() {
rep1(i, n) {
adj2[i].clear();
vis[i] = 0;
}
topo.clear();
priority_queue< pll, vector<pll>, greater<pll> > pq;
vector<ll> dis_s(n + 5, inf2), dis_t(n + 5, inf2), dis_x(n + 5, inf2), dis_y(n + 5, inf2);
dijkstra(s, dis_s);
dijkstra(t, dis_t);
dijkstra(x, dis_x);
dijkstra(y, dis_y);
rep1(u, n) {
for (auto [v, w] : adj[u]) {
if (dis_s[u] + w == dis_s[v]) {
if (dis_s[u] + w + dis_t[v] == dis_s[t]) {
adj2[u].pb(v);
}
}
}
}
rep1(i, n) vis[i] = 0;
rep1(i, n) {
if (vis[i]) conts;
dfs(i);
}
reverse(all(topo));
// dp_x[i] = min(dis_x[j]) for all j such that edge j->i exists in dag
// dp_y[i] = min(dis_y[j]) for all j such that edge i->j exists in dag
vector<ll> dp_x(n + 5, inf2), dp_y(n + 5, inf2);
trav(u, topo) {
amin(dp_x[u], dis_x[u]);
trav(v, adj2[u]) {
amin(dp_x[v], dp_x[u]);
}
}
reverse(all(topo));
trav(u, topo) {
amin(dp_y[u], dis_y[u]);
trav(v, adj2[u]) {
amin(dp_y[u], dp_y[v]);
}
}
ll ans = inf2;
rep1(u, n) {
ll cost = dp_x[u] + dp_y[u];
amin(ans, cost);
}
return ans;
};
ll ans = get();
swap(s, t);
amin(ans, get());
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
26628 KB |
Output is correct |
2 |
Correct |
453 ms |
25200 KB |
Output is correct |
3 |
Correct |
485 ms |
29404 KB |
Output is correct |
4 |
Correct |
423 ms |
26308 KB |
Output is correct |
5 |
Correct |
451 ms |
25764 KB |
Output is correct |
6 |
Correct |
434 ms |
26516 KB |
Output is correct |
7 |
Correct |
463 ms |
26156 KB |
Output is correct |
8 |
Correct |
452 ms |
26120 KB |
Output is correct |
9 |
Correct |
421 ms |
25580 KB |
Output is correct |
10 |
Correct |
355 ms |
25640 KB |
Output is correct |
11 |
Correct |
180 ms |
20548 KB |
Output is correct |
12 |
Correct |
457 ms |
25472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
26436 KB |
Output is correct |
2 |
Correct |
480 ms |
26276 KB |
Output is correct |
3 |
Correct |
470 ms |
25992 KB |
Output is correct |
4 |
Correct |
478 ms |
26348 KB |
Output is correct |
5 |
Correct |
483 ms |
26348 KB |
Output is correct |
6 |
Correct |
465 ms |
28596 KB |
Output is correct |
7 |
Correct |
529 ms |
28496 KB |
Output is correct |
8 |
Correct |
477 ms |
25800 KB |
Output is correct |
9 |
Correct |
478 ms |
26440 KB |
Output is correct |
10 |
Correct |
485 ms |
26228 KB |
Output is correct |
11 |
Correct |
207 ms |
22096 KB |
Output is correct |
12 |
Correct |
452 ms |
28436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
6736 KB |
Output is correct |
2 |
Correct |
5 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5076 KB |
Output is correct |
4 |
Correct |
20 ms |
8512 KB |
Output is correct |
5 |
Correct |
13 ms |
6768 KB |
Output is correct |
6 |
Correct |
5 ms |
5076 KB |
Output is correct |
7 |
Correct |
7 ms |
5168 KB |
Output is correct |
8 |
Correct |
6 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5176 KB |
Output is correct |
10 |
Correct |
11 ms |
6704 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
5 ms |
5076 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
5 ms |
5044 KB |
Output is correct |
15 |
Correct |
5 ms |
5044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
26628 KB |
Output is correct |
2 |
Correct |
453 ms |
25200 KB |
Output is correct |
3 |
Correct |
485 ms |
29404 KB |
Output is correct |
4 |
Correct |
423 ms |
26308 KB |
Output is correct |
5 |
Correct |
451 ms |
25764 KB |
Output is correct |
6 |
Correct |
434 ms |
26516 KB |
Output is correct |
7 |
Correct |
463 ms |
26156 KB |
Output is correct |
8 |
Correct |
452 ms |
26120 KB |
Output is correct |
9 |
Correct |
421 ms |
25580 KB |
Output is correct |
10 |
Correct |
355 ms |
25640 KB |
Output is correct |
11 |
Correct |
180 ms |
20548 KB |
Output is correct |
12 |
Correct |
457 ms |
25472 KB |
Output is correct |
13 |
Correct |
475 ms |
26436 KB |
Output is correct |
14 |
Correct |
480 ms |
26276 KB |
Output is correct |
15 |
Correct |
470 ms |
25992 KB |
Output is correct |
16 |
Correct |
478 ms |
26348 KB |
Output is correct |
17 |
Correct |
483 ms |
26348 KB |
Output is correct |
18 |
Correct |
465 ms |
28596 KB |
Output is correct |
19 |
Correct |
529 ms |
28496 KB |
Output is correct |
20 |
Correct |
477 ms |
25800 KB |
Output is correct |
21 |
Correct |
478 ms |
26440 KB |
Output is correct |
22 |
Correct |
485 ms |
26228 KB |
Output is correct |
23 |
Correct |
207 ms |
22096 KB |
Output is correct |
24 |
Correct |
452 ms |
28436 KB |
Output is correct |
25 |
Correct |
13 ms |
6736 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
20 ms |
8512 KB |
Output is correct |
29 |
Correct |
13 ms |
6768 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
7 ms |
5168 KB |
Output is correct |
32 |
Correct |
6 ms |
5204 KB |
Output is correct |
33 |
Correct |
5 ms |
5176 KB |
Output is correct |
34 |
Correct |
11 ms |
6704 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5044 KB |
Output is correct |
39 |
Correct |
5 ms |
5044 KB |
Output is correct |
40 |
Correct |
416 ms |
26872 KB |
Output is correct |
41 |
Correct |
419 ms |
25576 KB |
Output is correct |
42 |
Correct |
426 ms |
25596 KB |
Output is correct |
43 |
Correct |
206 ms |
22852 KB |
Output is correct |
44 |
Correct |
212 ms |
22960 KB |
Output is correct |
45 |
Correct |
448 ms |
28460 KB |
Output is correct |
46 |
Correct |
454 ms |
28380 KB |
Output is correct |
47 |
Correct |
450 ms |
26276 KB |
Output is correct |
48 |
Correct |
230 ms |
20968 KB |
Output is correct |
49 |
Correct |
332 ms |
26492 KB |
Output is correct |
50 |
Correct |
457 ms |
27144 KB |
Output is correct |
51 |
Correct |
460 ms |
28432 KB |
Output is correct |