#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];
typedef long long ll;
// Structure of the node
struct Node
{
ll value;
struct Node *L, *R;
};
// Structure to get the newly formed
// node
struct Node *getnode()
{
struct Node *temp = new struct Node;
temp->value = 0;
temp->L = NULL;
temp->R = NULL;
return temp;
}
// Creating the Root node
struct Node *root;
// Function to perform the point update
// on the dynamic segment tree
void UpdateHelper(struct Node *curr, ll index,
ll L, ll R, ll val)
{
// If the index is not overlapping
// with the index
if (L > index || R < index)
return;
// If the index is completely overlapping
// with the index
if (L == R && L == index)
{
// Update the value of the node
// to the given value
curr->value += val;
return;
}
// Computing the middle index if none
// of the above base cases are satisfied
ll mid = L - (L - R) / 2;
ll sum1 = 0, sum2 = 0;
// If the index is in the left subtree
if (index <= mid)
{
// Create a new node if the left
// subtree is null
if (curr->L == NULL)
curr->L = getnode();
// Recursively call the function
// for the left subtree
UpdateHelper(curr->L, index, L, mid, val);
}
// If the index is in the right subtree
else
{
// Create a new node if the right
// subtree is null
if (curr->R == NULL)
curr->R = getnode();
// Recursively call the function
// for the right subtree
UpdateHelper(curr->R, index, mid + 1, R, val);
}
// Storing the sum of the left subtree
if (curr->L)
sum1 = curr->L->value;
// Storing the sum of the right subtree
if (curr->R)
sum2 = curr->R->value;
// Storing the sum of the children into
// the node's value
curr->value = sum1 + sum2;
return;
}
// Function to find the sum of the
// values given by the range
ll queryHelper(struct Node *curr, ll a,
ll b, ll L, ll R)
{
// Return 0 if the root is null
if (curr == NULL)
return 0;
// If the index is not overlapping
// with the index, then the node
// is not created. So sum is 0
if (L > b || R < a)
return 0;
// If the index is completely overlapping
// with the index, return the node's value
if (L >= a && R <= b)
return curr->value;
ll mid = L - (L - R) / 2;
// Return the sum of values stored
// at the node's children
return queryHelper(curr->L, a, b, L, mid) + queryHelper(curr->R, a, b, mid + 1, R);
}
// Function to call the queryHelper
// function to find the sum for
// the query
ll query(int L, int R)
{
return queryHelper(root, L, R, 0, 1e16);
}
// Function to call the UpdateHelper
// function for the point update
void update(int index, int value)
{
UpdateHelper(root, index, 0, 1e16, value);
}
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()
{
root = getnode();
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;
// cout << ans1 << endl;
// cout << "------" << endl;
// update(0, 1);
for (int i = 1; i <= n; i++)
{
if (tdist[i] <= 1e18)
update(tdist[i], 1);
// cout << tdist[i] << " ";
}
// cout << endl;
for (int i = 1; i <= n; i++)
{
// cout << sdist[i] << " ";
}
// cout << endl;
// cout << query(0, 2) << endl;
for (int i = 1; i <= n; i++)
{
// continue;
if (tdist[i] < 1e18)
update(tdist[i], -1);
int kk = query(0, k - l - sdist[i]);
ans += kk;
// cout << "for " << i << " we have " << kk << endl;
// cout << "ans = " << ans << endl;
}
root = getnode();
for (int i = 1; i <= n; i++)
{
if (sdist[i] <= 1e18)
update(sdist[i], 1);
// cout << query(0, 1) << endl;
}
for (int i = 1; i <= n; i++)
{
// continue;
if (sdist[i] < 1e18)
update(sdist[i], -1);
int kk = query(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... |