/*input
10 15
6 8
7 9
2 7 12
8 10 17
1 3 1
3 8 14
5 7 15
2 3 7
1 10 14
3 6 12
1 5 10
8 9 1
2 9 7
1 4 1
1 8 1
2 4 7
5 6 16
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const int MOD = 1000000007;
const ll INF = (ll)1e15;
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
int N, M, S, T, U, V;
vpi edges[MX];
ll minDist;
void find(int start, vl &dist) {
dist = vl(N, INF);
dist[start] = 0;
priority_queue<pl, vpl, greater<pl>> pq;
pq.push({0, start});
while (pq.size()) {
pl curr = pq.top(); pq.pop();
int c = (int)curr.y;
ll d = curr.x;
if (d > dist[c]) continue;
for (auto u : edges[c]) {
if (dist[u.x] > d + u.y) {
dist[u.x] = d + u.y;
pq.push({dist[u.x], u.x});
}
}
}
}
// dp[i] = min(fromU[j]) f.a. j on shortest path T-S to i
void doDP(int st, vl &dp, const vl &fromU, const vl &fromPir, const vl &fromKitas) {
dp = vl(N, INF);
dp[st] = fromU[st];
priority_queue<pl, vpl, greater<pl>> pq;
pq.push({dp[st], st});
while (pq.size()) {
pl curr = pq.top(); pq.pop();
int c = (int)curr.y;
ll d = curr.x;
if (d > dp[c]) continue;
for (auto u : edges[c]) {
// printf("d = %lld, u = %d %d, dist = %lld\n", d, u.x, u.y, d + u.y + fromKitas[u.x]);
if (fromPir[c] + u.y + fromKitas[u.x] == minDist and min(dp[c], fromU[u.x]) < dp[u.x]) {
dp[u.x] = min(dp[c], fromU[u.x]);
pq.push({dp[u.x], u.x});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> M >> S >> T >> U >> V;
S--; T--; U--; V--;
for (int i = 0; i < M; ++i)
{
int a, b, w;
cin >> a >> b >> w; a--; b--;
edges[a].emplace_back(b, w);
edges[b].emplace_back(a, w);
}
vl fromU, fromV;
vl fromT, fromS;
find(U, fromU);
find(V, fromV);
find(T, fromT);
find(S, fromS);
minDist = fromS[T];
// void doDP(int st, vl & dp, const vl & fromU, const vl & fromS) {
vl dpT, dpS;
doDP(T, dpT, fromU, fromT, fromS);
doDP(S, dpS, fromU, fromS, fromT);
// print(dpT);
// print(dpS);
vl dist(N, INF);
dist[T] = 0;
priority_queue<pl, vpl, greater<pl>> pq;
pq.push({0, T});
ll ats = fromU[V];
while (pq.size()) {
pl curr = pq.top(); pq.pop();
int c = (int)curr.y;
ll d = curr.x;
// printf("c = %d, fromV = %lld, dpt = %lld, dps = %lld, ats = %lld\n", c,
// fromV[c], dpT[c], dpS[c], ats);
ats = min(ats, fromV[c] + min(dpT[c], dpS[c]));
if (d > dist[c]) continue;
for (auto u : edges[c]) {
if (d + u.y + fromS[u.x] == minDist and dist[u.x] == INF) {
dist[u.x] = d + u.y;
pq.push({dist[u.x], u.x});
}
}
}
printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
18336 KB |
Output is correct |
2 |
Correct |
373 ms |
17728 KB |
Output is correct |
3 |
Correct |
404 ms |
17732 KB |
Output is correct |
4 |
Correct |
327 ms |
17540 KB |
Output is correct |
5 |
Correct |
380 ms |
17952 KB |
Output is correct |
6 |
Correct |
334 ms |
18424 KB |
Output is correct |
7 |
Correct |
391 ms |
17664 KB |
Output is correct |
8 |
Correct |
381 ms |
18052 KB |
Output is correct |
9 |
Correct |
344 ms |
18980 KB |
Output is correct |
10 |
Correct |
279 ms |
18892 KB |
Output is correct |
11 |
Correct |
157 ms |
13304 KB |
Output is correct |
12 |
Correct |
343 ms |
18712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
17468 KB |
Output is correct |
2 |
Correct |
389 ms |
17628 KB |
Output is correct |
3 |
Correct |
386 ms |
17688 KB |
Output is correct |
4 |
Correct |
381 ms |
17700 KB |
Output is correct |
5 |
Correct |
400 ms |
17824 KB |
Output is correct |
6 |
Correct |
392 ms |
17464 KB |
Output is correct |
7 |
Correct |
422 ms |
17560 KB |
Output is correct |
8 |
Correct |
386 ms |
17524 KB |
Output is correct |
9 |
Correct |
397 ms |
17656 KB |
Output is correct |
10 |
Correct |
388 ms |
17652 KB |
Output is correct |
11 |
Correct |
203 ms |
13332 KB |
Output is correct |
12 |
Correct |
396 ms |
17612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
3704 KB |
Output is correct |
2 |
Correct |
7 ms |
2680 KB |
Output is correct |
3 |
Correct |
7 ms |
2680 KB |
Output is correct |
4 |
Correct |
22 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
3708 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
8 ms |
2808 KB |
Output is correct |
8 |
Correct |
8 ms |
2808 KB |
Output is correct |
9 |
Correct |
7 ms |
2808 KB |
Output is correct |
10 |
Correct |
14 ms |
3704 KB |
Output is correct |
11 |
Correct |
6 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2680 KB |
Output is correct |
13 |
Correct |
6 ms |
2680 KB |
Output is correct |
14 |
Correct |
7 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
18336 KB |
Output is correct |
2 |
Correct |
373 ms |
17728 KB |
Output is correct |
3 |
Correct |
404 ms |
17732 KB |
Output is correct |
4 |
Correct |
327 ms |
17540 KB |
Output is correct |
5 |
Correct |
380 ms |
17952 KB |
Output is correct |
6 |
Correct |
334 ms |
18424 KB |
Output is correct |
7 |
Correct |
391 ms |
17664 KB |
Output is correct |
8 |
Correct |
381 ms |
18052 KB |
Output is correct |
9 |
Correct |
344 ms |
18980 KB |
Output is correct |
10 |
Correct |
279 ms |
18892 KB |
Output is correct |
11 |
Correct |
157 ms |
13304 KB |
Output is correct |
12 |
Correct |
343 ms |
18712 KB |
Output is correct |
13 |
Correct |
365 ms |
17468 KB |
Output is correct |
14 |
Correct |
389 ms |
17628 KB |
Output is correct |
15 |
Correct |
386 ms |
17688 KB |
Output is correct |
16 |
Correct |
381 ms |
17700 KB |
Output is correct |
17 |
Correct |
400 ms |
17824 KB |
Output is correct |
18 |
Correct |
392 ms |
17464 KB |
Output is correct |
19 |
Correct |
422 ms |
17560 KB |
Output is correct |
20 |
Correct |
386 ms |
17524 KB |
Output is correct |
21 |
Correct |
397 ms |
17656 KB |
Output is correct |
22 |
Correct |
388 ms |
17652 KB |
Output is correct |
23 |
Correct |
203 ms |
13332 KB |
Output is correct |
24 |
Correct |
396 ms |
17612 KB |
Output is correct |
25 |
Correct |
15 ms |
3704 KB |
Output is correct |
26 |
Correct |
7 ms |
2680 KB |
Output is correct |
27 |
Correct |
7 ms |
2680 KB |
Output is correct |
28 |
Correct |
22 ms |
4728 KB |
Output is correct |
29 |
Correct |
15 ms |
3708 KB |
Output is correct |
30 |
Correct |
7 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2808 KB |
Output is correct |
32 |
Correct |
8 ms |
2808 KB |
Output is correct |
33 |
Correct |
7 ms |
2808 KB |
Output is correct |
34 |
Correct |
14 ms |
3704 KB |
Output is correct |
35 |
Correct |
6 ms |
2808 KB |
Output is correct |
36 |
Correct |
6 ms |
2680 KB |
Output is correct |
37 |
Correct |
6 ms |
2680 KB |
Output is correct |
38 |
Correct |
7 ms |
2808 KB |
Output is correct |
39 |
Correct |
7 ms |
2808 KB |
Output is correct |
40 |
Correct |
322 ms |
19572 KB |
Output is correct |
41 |
Correct |
334 ms |
18596 KB |
Output is correct |
42 |
Correct |
335 ms |
18696 KB |
Output is correct |
43 |
Correct |
197 ms |
13432 KB |
Output is correct |
44 |
Correct |
214 ms |
13428 KB |
Output is correct |
45 |
Correct |
424 ms |
19552 KB |
Output is correct |
46 |
Correct |
450 ms |
19096 KB |
Output is correct |
47 |
Correct |
347 ms |
18232 KB |
Output is correct |
48 |
Correct |
232 ms |
12792 KB |
Output is correct |
49 |
Correct |
287 ms |
19208 KB |
Output is correct |
50 |
Correct |
566 ms |
18808 KB |
Output is correct |
51 |
Correct |
395 ms |
19392 KB |
Output is correct |