题解:P15203 [SWERC 2018] Travel Guide
CuteGirlGC · · 题解
:::info[AI 使用声明]
本题解使用 Deepseek 生成了
:::epigraph[] 大炮打蚊子。 :::
Solution
提供一种完全不用思考的做法。
显然,从
直接上 cdq,把所有的距离离散化一下然后上树状数组。这个随便做。
但是有可能会有距离
最终时间复杂度
然后下面讲一下
考虑如果一个三元组
考虑按字典序先排序。对于一个三元组
然后还是相同三元组的问题,随便处理一下就可以了。
Code
本人只写了
:::success[
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1e5 + 1;
int n, m, k, d[3][kMaxN], b[kMaxN << 2], t[kMaxN << 2];
vector<pair<int, int>> g[kMaxN];
struct node {
int x, w;
friend bool operator<(node a, node b) { return a.w > b.w; }
};
map<tuple<int, int, int>, int> mp;
struct point {
int x, y, z, c = 0;
} a[kMaxN];
int lowbit(int x) {
return x & -x;
}
void add(int x, int d) {
for (; x <= k; x += lowbit(x)) {
t[x] += d;
}
}
int query(int x) {
int r = 0;
for (; x; x -= lowbit(x)) {
r += t[x];
}
return r;
}
void dijkstra(int* d, int s) {
priority_queue<node> q;
vector<bool> f(n + 1);
q.push({s, 0});
fill(d + 1, d + n + 1, 1e9);
d[s] = 0;
while (q.size()) {
int x = q.top().x;
q.pop();
if (f[x]) {
continue;
}
f[x] = 1;
for (auto [i, w] : g[x]) {
if (d[i] > d[x] + w) {
d[i] = d[x] + w;
q.push({i, d[i]});
}
}
}
}
void solve(int l, int r) {
if (l == r) {
return;
}
int m = l + r >> 1;
solve(l, m), solve(m + 1, r);
int j = l;
for (int i = m + 1; i <= r; i++) {
while (j <= m && a[j].y <= a[i].y) {
add(a[j].z, 1), j++;
}
a[i].c |= min(1, query(a[i].z));
}
for (; j > l; j--) {
add(a[j - 1].z, -1);
}
sort(a + l, a + r + 1, [](point a, point b) {
return a.y < b.y || (a.y == b.y && a.z < b.z);
});
}
int main() {
cin >> n >> m;
for (int i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w, u++, v++;
g[u].push_back({v, w}), g[v].push_back({u, w});
}
dijkstra(d[0], 1), dijkstra(d[1], 2), dijkstra(d[2], 3);
for (int i = 1; i <= n; i++) {
a[i] = {d[0][i], d[1][i], d[2][i]};
b[++k] = a[i].x, b[++k] = a[i].y, b[++k] = a[i].z;
}
sort(b + 1, b + k + 1);
k = unique(b + 1, b + k + 1) - b - 1;
for (int i = 1; i <= n; i++) {
a[i].x = lower_bound(b + 1, b + k + 1, a[i].x) - b;
a[i].y = lower_bound(b + 1, b + k + 1, a[i].y) - b;
a[i].z = lower_bound(b + 1, b + k + 1, a[i].z) - b;
mp[make_tuple(a[i].x, a[i].y, a[i].z)]++;
}
sort(a + 1, a + n + 1, [](point a, point b) {
return a.x < b.x || (a.x == b.x && a.y < b.y) ||
(a.x == b.x && a.y == b.y && a.z < b.z);
});
solve(1, n);
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!a[i].c) {
cnt += mp[make_tuple(a[i].x, a[i].y, a[i].z)];
}
}
cout << cnt << '\n';
return 0;
}
:::
:::success[
优化了一下码风。
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using Pair = pair<LL, LL>;
const LL kInf = 1e18;
struct Edge {
LL to, w;
};
struct Point {
LL d0, d1, d2;
friend bool operator<(const Point &a, const Point &b) {
return tie(a.d0, a.d1, a.d2) < tie(b.d0, b.d1, b.d2);
}
};
struct FenwickMin {
LL siz;
vector<LL> bit;
FenwickMin(LL n) : siz(n), bit(n + 1, kInf) {}
void update(LL i, LL v) {
for (; i <= siz; i += (i & -i)) {
bit[i] = min(bit[i], v);
}
}
LL query(LL i) {
LL res = kInf;
for (; i; i -= (i & -i)) {
res = min(res, bit[i]);
}
return res;
}
};
LL n, m, ans;
vector<LL> d0, d1, d2, ys;
vector<Point> p;
vector<vector<Edge>> g;
vector<LL> dijkstra(LL src) {
vector<LL> dis(n, kInf);
priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
dis[src] = 0;
pq.emplace(0, src);
while (!pq.empty()) {
LL du = pq.top().first, u = pq.top().second;
pq.pop();
if (du != dis[u]) {
continue;
}
for (auto &e : g[u]) {
if (dis[e.to] > du + e.w) {
dis[e.to] = du + e.w;
pq.emplace(dis[e.to], e.to);
}
}
}
return dis;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
g.resize(n);
for (LL i = 1; i <= m; i++) {
LL a, b, w;
cin >> a >> b >> w;
g[a].emplace_back(Edge{b, w});
g[b].emplace_back(Edge{a, w});
}
d0 = dijkstra(0);
d1 = dijkstra(1);
d2 = dijkstra(2);
p.resize(n);
for (LL i = 0; i < n; i++) {
p[i] = {d0[i], d1[i], d2[i]};
ys.emplace_back(d1[i]);
}
sort(p.begin(), p.end());
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
FenwickMin fw(ys.size());
for (LL i = 0, j; i < n; i = j) {
j = i + 1;
while (j < n && p[j].d0 == p[i].d0 &&
p[j].d1 == p[i].d1 && p[j].d2 == p[i].d2) {
j++;
}
LL pos = lower_bound(ys.begin(), ys.end(), p[i].d1) - ys.begin() + 1;
if (fw.query(pos) > p[i].d2) {
ans += j - i;
}
fw.update(pos, p[i].d2);
}
cout << ans << '\n';
return 0;
}
:::