This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef tree<pair<ll, int>, null_type, less<pair<ll, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int MAXN = 2e5 + 5;
const ll INF = 1e18;
int n;
vector<pair<int, ll>> adj[MAXN];
void dijkstra(int startnode, vector<ll> &dist) {
dist.clear(); dist.resize(n, INF);
dist[startnode] = 0;
priority_queue<pair<ll, int>> pq;
pq.push({0, startnode});
while (pq.size()) {
pair<ll, int> cur = pq.top(); pq.pop();
ll curdist = -cur.first;
int curnode = cur.second;
if (dist[curnode] != curdist)
continue;
for (auto a : adj[curnode]) {
ll newdist = curdist + a.second;
if (dist[a.first] > newdist) {
dist[a.first] = newdist;
pq.push({-newdist, a.first});
}
}
}
}
int main() {
int m; cin >> n >> m;
int s, t; ll l, k; cin >> s >> t >> l >> k;
s --; t --;
while (m --) {
int a, b; ll c; cin >> a >> b >> c;
a --; b --;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
vector<ll> dist_forwards, dist_backwards;
dijkstra(s, dist_forwards);
dijkstra(t, dist_backwards);
if (dist_forwards[t] <= k) {
cout << (ll)(n * (ll)(n - 1)) / 2; return 0;
}
ll ans = 0;
ordered_set vals;
for (int i = 0; i < n; i ++)
vals.insert({dist_backwards[i], i});
vector<pair<ll, int>> ee;
for (int i = 0; i < n; i ++)
ee.push_back({dist_forwards[i], i});
sort(ee.begin(), ee.end());
for (int ii = 0; ii < n; ii ++) {
int i = ee[ii].second;
vals.erase({dist_backwards[i], i});
ll sofar = dist_forwards[i] + l;
ll req = k - sofar;
// cout << dist_backwards[i] << " " << sofar << " " << vals.order_of_key({req, 1e9}) << endl;
ll cur = vals.order_of_key({req, 1e9});
ans += cur;
}
cout << ans << 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... |