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>
using namespace std;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int s, t, l;
cin >> s >> t >> l;
--s; --t;
long long k;
cin >> k;
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
const long long inf = (long long) 1e18;
vector<vector<long long>> dist(2, vector<long long>(n));
auto Dijkstra = [&](int s, vector<long long>& d) {
d = vector<long long>(n, inf);
d[s] = 0;
set<pair<long long, int>> st;
st.emplace(0, s);
while (!st.empty()) {
auto it = st.begin();
int v = it->second;
st.erase(it);
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (d[u] > d[v] + w) {
if (d[u] != inf) {
st.erase({d[u], u});
}
d[u] = d[v] + w;
st.emplace(d[u], u);
}
}
}
};
Dijkstra(s, dist[0]);
Dijkstra(t, dist[1]);
if (dist[0][t] <= k) {
cout << n * 1LL * (n - 1) / 2 << '\n';
return 0;
}
// dist[0][i] + L <= K - dist[1][j]
vector<long long> xs;
for (int i = 0; i < n; i++) {
xs.push_back(dist[0][i] + l);
xs.push_back(k - dist[1][i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
int sz = (int) xs.size();
fenwick<int> fenw(sz);
long long res = 0;
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) {
return dist[0][i] < dist[0][j];
});
for (int i : order) {
res += fenw.get((int) (lower_bound(xs.begin(), xs.end(), k - dist[1][i]) - xs.begin()));
fenw.modify((int) (lower_bound(xs.begin(), xs.end(), dist[0][i] + l) - xs.begin()), 1);
}
cout << res << '\n';
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... |