// 別解
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
using P = pair<long long, int>;
struct edge{
int to;
long long cost;
};
template< typename S, S (*op)(S, S), S (*e)() >
struct segmenttree{
private:
int _n;
std::vector<S> node;
public:
segmenttree() = default;
segmenttree(int n){
_n = 1;
while(_n < n){
_n *= 2;
}
node.resize(2 * _n, e());
}
segmenttree(std::vector<S> &v){
int n = v.size();
_n = 1;
while(_n < n){
_n *= 2;
}
node.resize(2 * _n, e());
for(int i = 0; i < n; i++){
set(i, v[i]);
}
}
// [i] <- val
void set(int i, S val){
i += _n;
node[i] = val;
while(i > 1){
i >>= 1;
node[i] = op(node[2 * i], node[2 * i + 1]);
}
}
// [i] <- op([i],val)
void update(int i, S val){
i += _n;
node[i] = op(node[i], val);
while(i > 1){
i >>= 1;
node[i] = op(node[2 * i], node[2 * i + 1]);
}
}
S get(int i){
i += _n;
return node[i];
}
S prod(int l,int r){
S pdl=e(), pdr=e();
l += _n;
r += _n;
while(l < r){
if(l & 1){
pdl = op(pdl, node[l++]);
}
if(r & 1){
pdr = op(node[--r], pdr);
}
l >>= 1;
r >>= 1;
}
return op(pdl, pdr);
};
};
int op(int a, int b){
return a + b;
}
int e(){
return 0;
}
constexpr long long inf = 1e18;
int main(){
int n, m;
cin >> n >> m;
int s, t;
long long l, k;
cin >> s >> t >> l >> k;
s--;
t--;
vector<vector<edge>> g(n);
for(int i = 0; i < m; i++){
int a, b;
long long c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
priority_queue<P, vector<P>, greater<P>> que;
vector<long long> dist_from_s(n, inf), dist_from_t(n, inf);
dist_from_s[s] = 0;
que.push({0, s});
while(!que.empty()){
auto [dist, now] = que.top();
que.pop();
if(dist_from_s[now] < dist){
continue;
}
for(auto [nxt, c]: g[now]){
if(dist_from_s[nxt] > dist + c){
dist_from_s[nxt] = dist + c;
que.push({dist + c, nxt});
}
}
}
dist_from_t[t] = 0;
que.push({0, t});
while(!que.empty()){
auto [dist, now] = que.top();
que.pop();
if(dist_from_t[now] < dist){
continue;
}
for(auto [nxt, c]: g[now]){
if(dist_from_t[nxt] > dist + c){
dist_from_t[nxt] = dist + c;
que.push({dist + c, nxt});
}
}
}
if(dist_from_s[t] <= k){
cout << (long long)(n) * (n - 1) / 2 << endl;
return 0;
}
vector<long long> val = dist_from_s;
for(int i = 0; i < n; i++){
val.emplace_back(dist_from_t[i]);
}
sort(val.begin(), val.end());
map<long long, int> index;
for(int i = 0; i < 2 * n; i++){
index[val[i]] = i;
}
segmenttree<int, op, e> seg1(2 * n), seg2(2 * n);
long long ans = 0;
for(int i = 0; i < n; i++){
long long ok = k - l - dist_from_s[i];
ans += seg2.prod(0, lower_bound(val.begin(), val.end(), ok + 1) - val.begin());
ok = k - l - dist_from_t[i];
ans += seg1.prod(0, lower_bound(val.begin(), val.end(), ok + 1) - val.begin());
seg1.update(index[dist_from_s[i]], 1);
seg2.update(index[dist_from_t[i]], 1);
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
139 ms |
28448 KB |
Output is correct |
8 |
Correct |
157 ms |
28340 KB |
Output is correct |
9 |
Correct |
103 ms |
18736 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
108 ms |
24876 KB |
Output is correct |
15 |
Correct |
336 ms |
30524 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
884 KB |
Output is correct |
18 |
Correct |
266 ms |
36112 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
928 KB |
Output is correct |
23 |
Correct |
96 ms |
24776 KB |
Output is correct |
24 |
Correct |
281 ms |
30624 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
147 ms |
23736 KB |
Output is correct |
28 |
Correct |
126 ms |
20148 KB |
Output is correct |
29 |
Correct |
177 ms |
28224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
388 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
21 |
Correct |
0 ms |
356 KB |
Output is correct |
22 |
Correct |
0 ms |
356 KB |
Output is correct |
23 |
Correct |
0 ms |
360 KB |
Output is correct |
24 |
Correct |
0 ms |
356 KB |
Output is correct |
25 |
Correct |
0 ms |
356 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
388 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
21 |
Correct |
0 ms |
356 KB |
Output is correct |
22 |
Correct |
0 ms |
356 KB |
Output is correct |
23 |
Correct |
0 ms |
360 KB |
Output is correct |
24 |
Correct |
0 ms |
356 KB |
Output is correct |
25 |
Correct |
0 ms |
356 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
604 KB |
Output is correct |
39 |
Correct |
3 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
4 ms |
856 KB |
Output is correct |
43 |
Correct |
7 ms |
1116 KB |
Output is correct |
44 |
Correct |
4 ms |
860 KB |
Output is correct |
45 |
Correct |
8 ms |
1116 KB |
Output is correct |
46 |
Correct |
5 ms |
1112 KB |
Output is correct |
47 |
Correct |
4 ms |
1116 KB |
Output is correct |
48 |
Correct |
4 ms |
1120 KB |
Output is correct |
49 |
Correct |
3 ms |
608 KB |
Output is correct |
50 |
Correct |
3 ms |
604 KB |
Output is correct |
51 |
Correct |
5 ms |
1116 KB |
Output is correct |
52 |
Correct |
3 ms |
860 KB |
Output is correct |
53 |
Correct |
5 ms |
1120 KB |
Output is correct |
54 |
Correct |
4 ms |
1120 KB |
Output is correct |
55 |
Correct |
3 ms |
868 KB |
Output is correct |
56 |
Correct |
3 ms |
792 KB |
Output is correct |
57 |
Correct |
5 ms |
604 KB |
Output is correct |
58 |
Correct |
3 ms |
816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
139 ms |
28448 KB |
Output is correct |
8 |
Correct |
157 ms |
28340 KB |
Output is correct |
9 |
Correct |
103 ms |
18736 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
108 ms |
24876 KB |
Output is correct |
15 |
Correct |
336 ms |
30524 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
884 KB |
Output is correct |
18 |
Correct |
266 ms |
36112 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
928 KB |
Output is correct |
23 |
Correct |
96 ms |
24776 KB |
Output is correct |
24 |
Correct |
281 ms |
30624 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
147 ms |
23736 KB |
Output is correct |
28 |
Correct |
126 ms |
20148 KB |
Output is correct |
29 |
Correct |
177 ms |
28224 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
388 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
356 KB |
Output is correct |
48 |
Correct |
0 ms |
360 KB |
Output is correct |
49 |
Correct |
0 ms |
356 KB |
Output is correct |
50 |
Correct |
0 ms |
356 KB |
Output is correct |
51 |
Correct |
0 ms |
356 KB |
Output is correct |
52 |
Correct |
0 ms |
360 KB |
Output is correct |
53 |
Correct |
0 ms |
356 KB |
Output is correct |
54 |
Correct |
0 ms |
356 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
2 ms |
860 KB |
Output is correct |
63 |
Correct |
2 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
3 ms |
860 KB |
Output is correct |
66 |
Correct |
3 ms |
860 KB |
Output is correct |
67 |
Correct |
3 ms |
604 KB |
Output is correct |
68 |
Correct |
3 ms |
860 KB |
Output is correct |
69 |
Correct |
2 ms |
604 KB |
Output is correct |
70 |
Correct |
2 ms |
604 KB |
Output is correct |
71 |
Correct |
4 ms |
856 KB |
Output is correct |
72 |
Correct |
7 ms |
1116 KB |
Output is correct |
73 |
Correct |
4 ms |
860 KB |
Output is correct |
74 |
Correct |
8 ms |
1116 KB |
Output is correct |
75 |
Correct |
5 ms |
1112 KB |
Output is correct |
76 |
Correct |
4 ms |
1116 KB |
Output is correct |
77 |
Correct |
4 ms |
1120 KB |
Output is correct |
78 |
Correct |
3 ms |
608 KB |
Output is correct |
79 |
Correct |
3 ms |
604 KB |
Output is correct |
80 |
Correct |
5 ms |
1116 KB |
Output is correct |
81 |
Correct |
3 ms |
860 KB |
Output is correct |
82 |
Correct |
5 ms |
1120 KB |
Output is correct |
83 |
Correct |
4 ms |
1120 KB |
Output is correct |
84 |
Correct |
3 ms |
868 KB |
Output is correct |
85 |
Correct |
3 ms |
792 KB |
Output is correct |
86 |
Correct |
5 ms |
604 KB |
Output is correct |
87 |
Correct |
3 ms |
816 KB |
Output is correct |
88 |
Correct |
242 ms |
18980 KB |
Output is correct |
89 |
Correct |
231 ms |
19088 KB |
Output is correct |
90 |
Correct |
311 ms |
30276 KB |
Output is correct |
91 |
Correct |
125 ms |
10972 KB |
Output is correct |
92 |
Correct |
112 ms |
10836 KB |
Output is correct |
93 |
Correct |
165 ms |
24760 KB |
Output is correct |
94 |
Correct |
144 ms |
24928 KB |
Output is correct |
95 |
Correct |
626 ms |
50352 KB |
Output is correct |
96 |
Correct |
273 ms |
19796 KB |
Output is correct |
97 |
Correct |
830 ms |
54820 KB |
Output is correct |
98 |
Correct |
863 ms |
55180 KB |
Output is correct |
99 |
Correct |
388 ms |
53816 KB |
Output is correct |
100 |
Correct |
356 ms |
53816 KB |
Output is correct |
101 |
Correct |
125 ms |
11096 KB |
Output is correct |
102 |
Correct |
160 ms |
10700 KB |
Output is correct |
103 |
Correct |
132 ms |
24944 KB |
Output is correct |
104 |
Correct |
152 ms |
24840 KB |
Output is correct |
105 |
Correct |
690 ms |
50380 KB |
Output is correct |
106 |
Correct |
701 ms |
50448 KB |
Output is correct |
107 |
Correct |
702 ms |
57120 KB |
Output is correct |
108 |
Correct |
740 ms |
56968 KB |
Output is correct |
109 |
Correct |
275 ms |
30896 KB |
Output is correct |
110 |
Correct |
233 ms |
30892 KB |
Output is correct |
111 |
Correct |
227 ms |
32608 KB |
Output is correct |
112 |
Correct |
230 ms |
29748 KB |
Output is correct |