Travel Guide(cdq+最短路)
题意
给定\(n\)个点,\(m\)条边。每个点到\(0\),\(1\),\(2\)号点都有最短路dist0,dist1,dist2。对于一个点\(A\),若存在点\(B\),\(B\)的dist0,dist1,dist2都小于等于\(A\),并且至少有一个距离要严格小于点\(A\)的,那么称点\(A\)为没用的点。不是没用的点,则为有用的点。求总共有多少个有用的点。
数据范围
\(4 \leq n \leq 100000\)
\(m \leq 500000\)
\(1 \leq w \leq 100\)
思路
首先,这三个距离特别容易算出来,跑三遍Dijkstra即可。
根据条件,\(B\)的dist0,dist1,dist2都小于等于\(A\),并且至少有一个距离要严格小于点\(A\)的,这是一个三维偏序问题,直接使用cdq分治。
答案怎么统计呢,对于点\(A\),我们找有多少个\(B\)是满足上述条件的,如果\(B\)的个数是大于\(0\)的,那么就说明\(A\)是没用的点。
考虑到存在三个距离相等的点,因此答案加上三个距离等于点\(A\)的点数即可。
这里统计\(B\)的个数,用树状数组维护。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> pii;
const int N = 100010, M = 1000010;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
int dist[N];
vector<int> nums;
struct Node
{
int a, b, c, s, id;
bool operator < (const Node &t) const
{
if(a != t.a) return a < t.a;
if(b != t.b) return b < t.b;
return c < t.c;
}
bool operator == (const Node &t) const
{
return a == t.a && b == t.b && c == t.c;
}
}q[N], tmp[N];
int is_ok[N];
int tree[N];
int lowbit(int x)
{
return x & -x;
}
void update(int x, int v)
{
for(int i = x; i < N; i += lowbit(i)) tree[i] += v;
}
int query(int x)
{
int res = 0;
for(int i = x; i; i -= lowbit(i)) res += tree[i];
return res;
}
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int find(int x)
{
return lower_bound(nums.begin(), nums.end(), x) - nums.begin();
}
void dijkstra(int u)
{
memset(st, 0, sizeof(st));
memset(dist, 0x3f, sizeof(dist));
priority_queue<pii, vector<pii>, greater<pii> > heap;
heap.push({0, u});
dist[u] = 0;
while(heap.size()) {
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; ~i; i = ne[i]) {
int j = e[i];
if(dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
heap.push({dist[j], j});
}
}
}
}
void merge_sort(int l, int r)
{
if(l >= r) return;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0;
while(i <= mid && j <= r) {
if(q[i].b <= q[j].b) update(q[i].c, 1), tmp[k ++] = q[i ++];
else {
if(query(q[j].c)) is_ok[q[j].id] = q[j].s;
tmp[k ++] = q[j ++];
}
}
while(i <= mid) update(q[i].c, 1), tmp[k ++] = q[i ++];
while(j <= r) {
if(query(q[j].c)) is_ok[q[j].id] = q[j].s;
tmp[k ++] = q[j ++];
}
for(int i = l; i <= mid; i ++) update(q[i].c, -1);
for(int i = l, j = 0; j < k; i ++, j ++) q[i] = tmp[j];
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof(h));
for(int i = 0; i < m; i ++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
dijkstra(0);
for(int i = 0; i < n; i ++) q[i].a = dist[i];
dijkstra(1);
for(int i = 0; i < n; i ++) q[i].b = dist[i];
dijkstra(2);
for(int i = 0; i < n; i ++) nums.push_back(dist[i]);
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
for(int i = 0; i < n; i ++) q[i].c = find(dist[i]) + 1;
for(int i = 0; i < n; i ++) q[i].s = 1;
sort(q, q + n);
int k = 1;
for(int i = 1; i < n; i ++) {
if(q[i] == q[k - 1]) q[k - 1].s ++;
else q[k ++] = q[i];
}
for(int i = 0; i < k; i ++) q[i].id = i;
merge_sort(0, k - 1);
int ans = n;
for(int i = 0; i < k; i ++) ans -= is_ok[i];
printf("%d\n", ans);
return 0;
}