#include <bits/stdc++.h>
#define pi pair<int, int>
#define int long long
#define ff first
#define ss second
using namespace std;
const int N = 1ll << 18;
vector<pi> graph[N];
int value[N << 3];
void add(int i, int val, int cur = 1, int l = 0, int r = N)
{
if (i < l || i > r)
return;
if (l == r)
{
value[cur] += val;
return;
}
int lc = cur + cur, rc = lc + 1, mid = (l + r) / 2;
add(i, val, lc, l, mid);
add(i, val, rc, mid + 1, r);
value[cur] = value[lc] + value[rc];
}
int calculate(int l, int r, int cur = 1, int st = 0, int en = N)
{
if (r < st || l > en)
return 0;
if (st >= l && en <= r)
return value[cur];
int lc = cur + cur, rc = lc + 1, mid = (st + en) / 2;
return calculate(l, r, lc, st, mid) + calculate(l, r, rc, mid + 1, en);
}
vector<int> dijkstra(int start)
{
vector<int> dist(N, 1e18);
priority_queue<pi, vector<pi>, greater<pi>> q;
q.push({0, start});
dist[start] = 0;
int node, weight;
while (q.size())
{
node = q.top().ss;
q.pop();
for (auto i : graph[node])
{
if (dist[i.ss] > dist[node] + i.ff)
{
dist[i.ss] = dist[node] + i.ff;
q.push({dist[i.ss], i.ss});
}
}
}
return dist;
}
void solve()
{
int n, m;
cin >> n >> m;
int s, t, l, k;
cin >> s >> t >> l >> k;
int u, v, w;
for (int i = 0; i < m; i++)
{
cin >> u >> v >> w;
graph[u].push_back({w, v});
graph[v].push_back({w, u});
}
vector<int> sdist = dijkstra(s);
vector<int> tdist = dijkstra(t);
if (sdist[t] <= k)
{
cout << n * (n - 1) / 2;
return;
}
int ans1 = 0;
int ans = 0;
// add(0, 1);
for (int i = 1; i <= n; i++)
{
if (tdist[i] <= 1e18)
add(tdist[i], 1);
// cout << calculate(0, 1) << endl;
}
for (int i = 1; i <= n; i++)
{
// continue;
if (tdist[i] <= n)
add(tdist[i], -1);
int kk = calculate(0, k - l - sdist[i]);
ans += kk;
// cout << "for " << i << " we have " << kk << endl;
// cout << "ans = " << ans << endl;
}
for (int i = 1; i <= n; i++)
{
if (sdist[i] <= 1e18)
add(sdist[i], 1);
// cout << calculate(0, 1) << endl;
}
for (int i = 1; i <= n; i++)
{
// continue;
if (sdist[i] <= n)
add(sdist[i], -1);
int kk = calculate(0, k - l - tdist[i]);
ans += kk;
// cout << "for " << i << " we have " << kk << endl;
// cout << "ans = " << ans << endl;
}
cout << ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
{
solve();
cout << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |