Bzoj4016: [FJOI2014]最短路径树问题
题面
Sol
先\(SPFA\)求出单源最短路,\(Bfs\)建出树,字典序可以用堆解决
然后就是点分治的一眼题
开桶记录到当前根经过边长度相同的最长路,记录它的长度
自己强行\(yy\)了一个这种类型的点分丑陋写法
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 5);
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, k, mx[_], root, size[_], vis[_], tot, dis[_];
struct Edge{
int to[_], w[_], next[_], first[_], cnt;
IL void Init(){
Fill(first, -1);
}
IL void Add(RG int u, RG int v, RG int ww){
to[cnt] = v, next[cnt] = first[u], w[cnt] = ww, first[u] = cnt++;
}
} G1, G2;
int deep[_], len[_], t[_], num[_], S1[_], S2[_];
ll ans1, ans2;
queue <int> Q;
priority_queue <int> H;
IL void GetRoot(RG int u, RG int ff){
size[u] = 1, mx[u] = 0;
for(RG int e = G2.first[u]; e != -1; e = G2.next[e]){
RG int v = G2.to[e];
if(vis[v] || v == ff) continue;
GetRoot(v, u);
size[u] += size[v];
mx[u] = max(mx[u], size[v]);
}
mx[u] = max(mx[u], tot - size[u]);
if(mx[u] < mx[root]) root = u;
}
IL void GetDeep(RG int u, RG int ff, RG int dd, RG int nn){
if(nn > k) return;
S1[++S1[0]] = u, S2[++S2[0]] = u, deep[u] = dd, len[u] = nn;
for(RG int e = G2.first[u]; e != -1; e = G2.next[e]){
RG int v = G2.to[e], w = G2.w[e];
if(vis[v] || v == ff) continue;
GetDeep(v, u, dd + w, nn + 1);
}
}
IL void Solve(RG int u){
vis[u] = 1, t[0] = 0, num[0] = 1;
for(RG int e = G2.first[u]; e != -1; e = G2.next[e]){
RG int v = G2.to[e];
if(vis[v]) continue;
GetDeep(v, u, G2.w[e], 1);
for(RG int i = 1; i <= S1[0]; ++i){
RG int dd = deep[S1[i]], nn = k - len[S1[i]];
if(!num[nn]) continue;
if(t[nn] + dd > ans1) ans1 = t[nn] + dd, ans2 = num[nn];
else if(t[nn] + dd == ans1) ans2 += num[nn];
}
for(; S1[0]; --S1[0]){
RG int dd = deep[S1[S1[0]]], nn = len[S1[S1[0]]];
if(dd > t[nn]) t[nn] = dd, num[nn] = 1;
else if(dd == t[nn]) num[nn]++;
}
}
for(; S2[0]; --S2[0]){
RG int dd = deep[S2[S2[0]]], nn = len[S2[S2[0]]];
t[nn] = num[nn] = 0;
}
for(RG int e = G2.first[u]; e != -1; e = G2.next[e]){
RG int v = G2.to[e];
if(vis[v]) continue;
root = 0, tot = size[v];
GetRoot(v, u), Solve(root);
}
}
IL void SPFA(){
Fill(dis, 127);
Q.push(1), vis[1] = 1, dis[1] = 0;
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = G1.first[u]; e != -1; e = G1.next[e]){
RG int v = G1.to[e], w = G1.w[e];
if(dis[u] + w < dis[v]){
dis[v] = dis[u] + w;
if(!vis[v]) vis[v] = 1, Q.push(v);
}
}
vis[u] = 0;
}
}
IL void Build(){
H.push(-1), Fill(vis, 0), vis[1] = 1;
while(!H.empty()){
RG int u = -H.top(); H.pop();
for(RG int e = G1.first[u]; e != -1; e = G1.next[e]){
RG int v = G1.to[e], w = G1.w[e];
if(vis[v] || dis[v] != dis[u] + w) continue;
G2.Add(u, v, w), G2.Add(v, u, w);
vis[v] = 1, H.push(-v);
}
}
}
int main(RG int argc, RG char* argv[]){
tot = n = Input(), m = Input(), k = Input() - 1;
G1.Init(), G2.Init();
for(RG int i = 1; i <= m; ++i){
RG int u = Input(), v = Input(), w = Input();
G1.Add(u, v, w), G1.Add(v, u, w);
}
SPFA(), Build(), Fill(vis, 0);
mx[0] = n + 1, GetRoot(1, 0),Solve(root);
printf("%lld %lld\n", ans1, ans2);
return 0;
}