#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define sz(v) (int)v.size()
#define ps(y) cout << fixed << setprecision(y)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define trav(x, v) for(auto &x: v)
#define w(t) int t; cin >> t; while(t--)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = a; i >= b; i--)
#define rep0(i, n) rep(i, 0, n - 1)
#define rrep0(i, n) rrep(i, n - 1, 0)
#define rep1(i, n) rep(i, 1, n)
#define rrep1(i, n) rrep(i, n, 1)
#define inp(arr, n) rep0(i, n) cin >> arr[i];
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef map<char, ll> mci;
typedef priority_queue<ll> pq_mx;
typedef priority_queue<pii, vp, greater<>> pq_mn;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
* find_by_order(i) -> returns an iterator to the element at ith position (0 based)
* order_of_key(i) -> returns the position of element i (0 based)
*/
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll inf = 1e18;
const double eps = 1e-10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void fio() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
}
int n, m, S, T, U, V;
ll dp[2][N], res;
vi du, dv;
vp gr[N];
vi dijkstra(int src) {
vi dist(n + 1, inf);
dist[src] = 0;
set<pii> q;
q.emplace(0, src);
while (!q.empty()) {
auto[d, cur] = *q.begin();
q.erase(q.begin());
trav(neigh, gr[cur]) {
if (dist[neigh.ff] > d + neigh.ss) {
auto it = q.find({dist[neigh.ff], neigh.ff});
if (it != q.end()) q.erase(it);
dist[neigh.ff] = d + neigh.ss, q.emplace(dist[neigh.ff], neigh.ff);
}
}
}
return dist;
}
void f(ll s, ll e) {
rep0(i, 2) {
rep1(x, n) dp[i][x] = inf;
}
vi dist(n + 1, inf);
pq_mn q;
q.emplace(0, s), dist[s] = 0, dp[0][s] = du[s], dp[1][s] = dv[s];
while (!q.empty()) {
auto[d, cur] = q.top();
q.pop();
if (dist[cur] < d) continue;
trav(neigh, gr[cur]) {
if (dist[neigh.ff] > d + neigh.ss) {
dist[neigh.ff] = d + neigh.ss, q.emplace(dist[neigh.ff], neigh.ff);
dp[0][neigh.ff] = min(du[neigh.ff], dp[0][cur]);
dp[1][neigh.ff] = min(dv[neigh.ff], dp[1][cur]);
} else if (dist[neigh.ff] == d + neigh.ss and
min(du[neigh.ff], dp[0][cur]) + min(dv[neigh.ff], dp[1][cur]) <
dp[0][neigh.ff] + dp[1][neigh.ff]) {
dp[0][neigh.ff] = min(du[neigh.ff], dp[0][cur]);
dp[1][neigh.ff] = min(dv[neigh.ff], dp[1][cur]);
}
}
}
res = min(res, dp[0][e] + dp[1][e]);
}
int main() {
fio();
cin >> n >> m >> S >> T >> U >> V;
rep0(x, m) {
int u, v, w;
cin >> u >> v >> w;
gr[u].eb(v, w), gr[v].eb(u, w);
}
du = dijkstra(U), dv = dijkstra(V);
res = du[V];
f(S, T), f(T, S);
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
25664 KB |
Output is correct |
2 |
Correct |
421 ms |
24324 KB |
Output is correct |
3 |
Correct |
381 ms |
25124 KB |
Output is correct |
4 |
Correct |
398 ms |
25052 KB |
Output is correct |
5 |
Correct |
390 ms |
23332 KB |
Output is correct |
6 |
Correct |
463 ms |
26100 KB |
Output is correct |
7 |
Correct |
415 ms |
23968 KB |
Output is correct |
8 |
Correct |
446 ms |
23976 KB |
Output is correct |
9 |
Correct |
419 ms |
25196 KB |
Output is correct |
10 |
Correct |
301 ms |
25128 KB |
Output is correct |
11 |
Correct |
142 ms |
15536 KB |
Output is correct |
12 |
Correct |
490 ms |
24248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
435 ms |
23888 KB |
Output is correct |
2 |
Correct |
469 ms |
23428 KB |
Output is correct |
3 |
Correct |
421 ms |
23516 KB |
Output is correct |
4 |
Correct |
446 ms |
23636 KB |
Output is correct |
5 |
Correct |
502 ms |
23600 KB |
Output is correct |
6 |
Correct |
402 ms |
24308 KB |
Output is correct |
7 |
Correct |
501 ms |
23628 KB |
Output is correct |
8 |
Correct |
469 ms |
23780 KB |
Output is correct |
9 |
Correct |
423 ms |
23492 KB |
Output is correct |
10 |
Correct |
492 ms |
23452 KB |
Output is correct |
11 |
Correct |
147 ms |
15480 KB |
Output is correct |
12 |
Correct |
399 ms |
23760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
6732 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
5 ms |
5068 KB |
Output is correct |
4 |
Correct |
21 ms |
8512 KB |
Output is correct |
5 |
Correct |
11 ms |
6736 KB |
Output is correct |
6 |
Correct |
5 ms |
5164 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
5 ms |
5272 KB |
Output is correct |
9 |
Correct |
5 ms |
5068 KB |
Output is correct |
10 |
Correct |
12 ms |
6732 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
25664 KB |
Output is correct |
2 |
Correct |
421 ms |
24324 KB |
Output is correct |
3 |
Correct |
381 ms |
25124 KB |
Output is correct |
4 |
Correct |
398 ms |
25052 KB |
Output is correct |
5 |
Correct |
390 ms |
23332 KB |
Output is correct |
6 |
Correct |
463 ms |
26100 KB |
Output is correct |
7 |
Correct |
415 ms |
23968 KB |
Output is correct |
8 |
Correct |
446 ms |
23976 KB |
Output is correct |
9 |
Correct |
419 ms |
25196 KB |
Output is correct |
10 |
Correct |
301 ms |
25128 KB |
Output is correct |
11 |
Correct |
142 ms |
15536 KB |
Output is correct |
12 |
Correct |
490 ms |
24248 KB |
Output is correct |
13 |
Correct |
435 ms |
23888 KB |
Output is correct |
14 |
Correct |
469 ms |
23428 KB |
Output is correct |
15 |
Correct |
421 ms |
23516 KB |
Output is correct |
16 |
Correct |
446 ms |
23636 KB |
Output is correct |
17 |
Correct |
502 ms |
23600 KB |
Output is correct |
18 |
Correct |
402 ms |
24308 KB |
Output is correct |
19 |
Correct |
501 ms |
23628 KB |
Output is correct |
20 |
Correct |
469 ms |
23780 KB |
Output is correct |
21 |
Correct |
423 ms |
23492 KB |
Output is correct |
22 |
Correct |
492 ms |
23452 KB |
Output is correct |
23 |
Correct |
147 ms |
15480 KB |
Output is correct |
24 |
Correct |
399 ms |
23760 KB |
Output is correct |
25 |
Correct |
12 ms |
6732 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
21 ms |
8512 KB |
Output is correct |
29 |
Correct |
11 ms |
6736 KB |
Output is correct |
30 |
Correct |
5 ms |
5164 KB |
Output is correct |
31 |
Correct |
4 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5272 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
12 ms |
6732 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
538 ms |
26124 KB |
Output is correct |
41 |
Correct |
385 ms |
24980 KB |
Output is correct |
42 |
Correct |
405 ms |
24964 KB |
Output is correct |
43 |
Correct |
153 ms |
15428 KB |
Output is correct |
44 |
Correct |
149 ms |
15368 KB |
Output is correct |
45 |
Correct |
364 ms |
22736 KB |
Output is correct |
46 |
Correct |
316 ms |
23372 KB |
Output is correct |
47 |
Correct |
459 ms |
25488 KB |
Output is correct |
48 |
Correct |
141 ms |
15420 KB |
Output is correct |
49 |
Correct |
345 ms |
25724 KB |
Output is correct |
50 |
Correct |
362 ms |
23208 KB |
Output is correct |
51 |
Correct |
316 ms |
23256 KB |
Output is correct |