#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 200005
int numNode, numEdge;
int from, to, length, minTime;
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){
return (x ^ u ^ v);
}
} edge[MAX];
vector<int> G[MAX];
void dijkstra(int s, int minDist[]){
for (int i = 1; i <= numNode; ++i) minDist[i] = LINF;
minDist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.emplace(0, s);
while(q.size()){
int du, u; tie(du, u) = q.top();
q.pop();
if (du > minDist[u]) continue;
for (int&i : G[u]){
int v = edge[i].other(u);
if(minimize(minDist[v], minDist[u] + edge[i].w)){
q.emplace(minDist[v], v);
}
}
}
}
int minDist[2][MAX];
vector<int> comp;
struct FenwickTree{
vector<int> F;
int n;
FenwickTree(int _n = 0){
this -> n = _n;
F.resize(n + 5, 0);
}
int low_bit(int p){
return p & -p;
}
void upd(int p, int v){
for (; p <= n; p += low_bit(p)) F[p] += v;
}
int query(int p){
int res = 0;
for (; p > 0; p -= low_bit(p)) res += F[p];
return res;
}
};
int find(int x){
return lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1;
}
void process(void){
cin >> numNode >> numEdge >> from >> to;
cin >> length >> minTime;
//build a new road with length l
//move from s -> t and does not exceed minTime
//D(from, u) + l + D(v, to) <= minTime
//-> D(from, u) <= minTime - l - D(v, to)
for (int i = 1; i <= numEdge; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
dijkstra(from, minDist[0]);
dijkstra(to, minDist[1]);
if(minDist[0][to] <= minTime){
return void(cout << numNode * (numNode - 1) / 2);
}
for (int i = 1; i <= numNode; ++i){
comp.emplace_back(minDist[0][i]);
comp.emplace_back(minTime - minDist[1][i] - length);
}
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
FenwickTree ft((int)comp.size());
for (int i = 1; i <= numNode; ++i){
int value = find(minDist[0][i]);
ft.upd(value, 1);
}
int ans = 0;
for (int i = 1; i <= numNode; ++i){
int prv = find(minDist[0][i]);
int value = find(minTime - minDist[1][i] - length);
ft.upd(prv, -1);
ans += ft.query(value);
ft.upd(prv, 1);
}
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5468 KB |
Output is correct |
7 |
Correct |
62 ms |
24356 KB |
Output is correct |
8 |
Correct |
74 ms |
24228 KB |
Output is correct |
9 |
Correct |
68 ms |
22108 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
4 ms |
5464 KB |
Output is correct |
14 |
Correct |
41 ms |
18900 KB |
Output is correct |
15 |
Correct |
145 ms |
23996 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
4 ms |
5464 KB |
Output is correct |
18 |
Correct |
97 ms |
24036 KB |
Output is correct |
19 |
Correct |
2 ms |
4952 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
6 ms |
5468 KB |
Output is correct |
23 |
Correct |
41 ms |
18892 KB |
Output is correct |
24 |
Correct |
164 ms |
24224 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
74 ms |
27828 KB |
Output is correct |
28 |
Correct |
56 ms |
19704 KB |
Output is correct |
29 |
Correct |
81 ms |
21296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
2 ms |
4952 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
4 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
5032 KB |
Output is correct |
24 |
Correct |
3 ms |
4952 KB |
Output is correct |
25 |
Correct |
2 ms |
4952 KB |
Output is correct |
26 |
Correct |
3 ms |
4952 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
2 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
2 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
2 ms |
4952 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
4 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
5032 KB |
Output is correct |
24 |
Correct |
3 ms |
4952 KB |
Output is correct |
25 |
Correct |
2 ms |
4952 KB |
Output is correct |
26 |
Correct |
3 ms |
4952 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
2 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
2 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
4956 KB |
Output is correct |
33 |
Correct |
3 ms |
5468 KB |
Output is correct |
34 |
Correct |
3 ms |
5468 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
4 ms |
5468 KB |
Output is correct |
37 |
Correct |
3 ms |
5468 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5468 KB |
Output is correct |
40 |
Correct |
3 ms |
5464 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
4 ms |
5404 KB |
Output is correct |
44 |
Correct |
3 ms |
5468 KB |
Output is correct |
45 |
Correct |
5 ms |
5468 KB |
Output is correct |
46 |
Correct |
4 ms |
5468 KB |
Output is correct |
47 |
Correct |
4 ms |
5468 KB |
Output is correct |
48 |
Correct |
3 ms |
5468 KB |
Output is correct |
49 |
Correct |
3 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5212 KB |
Output is correct |
51 |
Correct |
6 ms |
5468 KB |
Output is correct |
52 |
Correct |
5 ms |
5468 KB |
Output is correct |
53 |
Correct |
4 ms |
5512 KB |
Output is correct |
54 |
Correct |
6 ms |
5536 KB |
Output is correct |
55 |
Correct |
3 ms |
5212 KB |
Output is correct |
56 |
Correct |
3 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5468 KB |
Output is correct |
7 |
Correct |
62 ms |
24356 KB |
Output is correct |
8 |
Correct |
74 ms |
24228 KB |
Output is correct |
9 |
Correct |
68 ms |
22108 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
4 ms |
5464 KB |
Output is correct |
14 |
Correct |
41 ms |
18900 KB |
Output is correct |
15 |
Correct |
145 ms |
23996 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
4 ms |
5464 KB |
Output is correct |
18 |
Correct |
97 ms |
24036 KB |
Output is correct |
19 |
Correct |
2 ms |
4952 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
6 ms |
5468 KB |
Output is correct |
23 |
Correct |
41 ms |
18892 KB |
Output is correct |
24 |
Correct |
164 ms |
24224 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
74 ms |
27828 KB |
Output is correct |
28 |
Correct |
56 ms |
19704 KB |
Output is correct |
29 |
Correct |
81 ms |
21296 KB |
Output is correct |
30 |
Correct |
2 ms |
4952 KB |
Output is correct |
31 |
Correct |
2 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
4956 KB |
Output is correct |
33 |
Correct |
2 ms |
4956 KB |
Output is correct |
34 |
Correct |
3 ms |
4956 KB |
Output is correct |
35 |
Correct |
2 ms |
4956 KB |
Output is correct |
36 |
Correct |
3 ms |
4996 KB |
Output is correct |
37 |
Correct |
2 ms |
4952 KB |
Output is correct |
38 |
Correct |
2 ms |
4956 KB |
Output is correct |
39 |
Correct |
2 ms |
4956 KB |
Output is correct |
40 |
Correct |
2 ms |
4956 KB |
Output is correct |
41 |
Correct |
2 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
4956 KB |
Output is correct |
43 |
Correct |
2 ms |
4956 KB |
Output is correct |
44 |
Correct |
4 ms |
4956 KB |
Output is correct |
45 |
Correct |
2 ms |
4956 KB |
Output is correct |
46 |
Correct |
2 ms |
4956 KB |
Output is correct |
47 |
Correct |
2 ms |
4956 KB |
Output is correct |
48 |
Correct |
2 ms |
4956 KB |
Output is correct |
49 |
Correct |
2 ms |
4956 KB |
Output is correct |
50 |
Correct |
2 ms |
4956 KB |
Output is correct |
51 |
Correct |
4 ms |
4956 KB |
Output is correct |
52 |
Correct |
2 ms |
5032 KB |
Output is correct |
53 |
Correct |
3 ms |
4952 KB |
Output is correct |
54 |
Correct |
2 ms |
4952 KB |
Output is correct |
55 |
Correct |
3 ms |
4952 KB |
Output is correct |
56 |
Correct |
2 ms |
4956 KB |
Output is correct |
57 |
Correct |
2 ms |
4956 KB |
Output is correct |
58 |
Correct |
2 ms |
4956 KB |
Output is correct |
59 |
Correct |
2 ms |
4956 KB |
Output is correct |
60 |
Correct |
3 ms |
4956 KB |
Output is correct |
61 |
Correct |
2 ms |
4956 KB |
Output is correct |
62 |
Correct |
3 ms |
5468 KB |
Output is correct |
63 |
Correct |
3 ms |
5468 KB |
Output is correct |
64 |
Correct |
3 ms |
5212 KB |
Output is correct |
65 |
Correct |
4 ms |
5468 KB |
Output is correct |
66 |
Correct |
3 ms |
5468 KB |
Output is correct |
67 |
Correct |
3 ms |
5212 KB |
Output is correct |
68 |
Correct |
3 ms |
5468 KB |
Output is correct |
69 |
Correct |
3 ms |
5464 KB |
Output is correct |
70 |
Correct |
2 ms |
5212 KB |
Output is correct |
71 |
Correct |
2 ms |
5212 KB |
Output is correct |
72 |
Correct |
4 ms |
5404 KB |
Output is correct |
73 |
Correct |
3 ms |
5468 KB |
Output is correct |
74 |
Correct |
5 ms |
5468 KB |
Output is correct |
75 |
Correct |
4 ms |
5468 KB |
Output is correct |
76 |
Correct |
4 ms |
5468 KB |
Output is correct |
77 |
Correct |
3 ms |
5468 KB |
Output is correct |
78 |
Correct |
3 ms |
5212 KB |
Output is correct |
79 |
Correct |
3 ms |
5212 KB |
Output is correct |
80 |
Correct |
6 ms |
5468 KB |
Output is correct |
81 |
Correct |
5 ms |
5468 KB |
Output is correct |
82 |
Correct |
4 ms |
5512 KB |
Output is correct |
83 |
Correct |
6 ms |
5536 KB |
Output is correct |
84 |
Correct |
3 ms |
5212 KB |
Output is correct |
85 |
Correct |
3 ms |
5212 KB |
Output is correct |
86 |
Correct |
3 ms |
5212 KB |
Output is correct |
87 |
Correct |
3 ms |
5212 KB |
Output is correct |
88 |
Correct |
83 ms |
20240 KB |
Output is correct |
89 |
Correct |
83 ms |
20276 KB |
Output is correct |
90 |
Correct |
92 ms |
24228 KB |
Output is correct |
91 |
Correct |
39 ms |
15584 KB |
Output is correct |
92 |
Correct |
39 ms |
15608 KB |
Output is correct |
93 |
Correct |
46 ms |
19124 KB |
Output is correct |
94 |
Correct |
46 ms |
18968 KB |
Output is correct |
95 |
Correct |
283 ms |
25800 KB |
Output is correct |
96 |
Correct |
146 ms |
21440 KB |
Output is correct |
97 |
Correct |
289 ms |
25524 KB |
Output is correct |
98 |
Correct |
229 ms |
25528 KB |
Output is correct |
99 |
Correct |
102 ms |
25400 KB |
Output is correct |
100 |
Correct |
104 ms |
25532 KB |
Output is correct |
101 |
Correct |
41 ms |
15452 KB |
Output is correct |
102 |
Correct |
41 ms |
15464 KB |
Output is correct |
103 |
Correct |
47 ms |
18888 KB |
Output is correct |
104 |
Correct |
64 ms |
18888 KB |
Output is correct |
105 |
Correct |
277 ms |
25796 KB |
Output is correct |
106 |
Correct |
258 ms |
25768 KB |
Output is correct |
107 |
Correct |
260 ms |
27964 KB |
Output is correct |
108 |
Correct |
309 ms |
27988 KB |
Output is correct |
109 |
Correct |
92 ms |
19708 KB |
Output is correct |
110 |
Correct |
86 ms |
19708 KB |
Output is correct |
111 |
Correct |
109 ms |
21240 KB |
Output is correct |
112 |
Correct |
112 ms |
21208 KB |
Output is correct |