• 【刷题】BZOJ 2599 [IOI2011]Race


    Description

    给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000

    Input

    第一行 两个整数 n, k

    第二..n行 每行三个整数 表示一条无向边的两端和权值 (注意点的编号从0开始)

    Output

    一个整数 表示最小边数量 如果不存在这样的路径 输出-1

    Sample Input

    4 3

    0 1 1

    1 2 2

    1 3 4

    Sample Output

    2

    Solution

    点分治

    考虑如何计算答案,有一个节点,我们依次遍历它的所有儿子,遍历到一个儿子时,求的是它与前面已经遍历过的子树一起的答案(即点对中有一点在当前遍历到的子树之中,另一点在以前已经遍历完的子树之中),这样保证了不需要去重,也保证了正确性

    开一个桶,(Mf[i])表示距离当前根 (i) 长度的最短深度是多少,每次更新答案就是 (dep[x]+Mf[dis[x]-dep[x]])

    在点分树中用memset会很慢,于是每次求完当前根的答案之后,用之前算答案的函数把 (Mf) 数组更新回去(实际上就是memset的效果),然后再下一步点分

    BZOJ上有边权等于0的,所以每次进solve的时候都要把 (Mf[0]) 赋为0

    #include<bits/stdc++.h>
    #define ui unsigned int
    #define ll long long
    #define db double
    #define ld long double
    #define ull unsigned long long
    const int MAXN=200000+10,MAXK=1000000+10,inf=0x3f3f3f3f;
    int Mf[MAXK],dep[MAXN],dis[MAXN],n,k,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],size[MAXN],Mx[MAXN],root,ans=inf,finish[MAXN];
    template<typename T> inline void read(T &x)
    {
    	T data=0,w=1;
    	char ch=0;
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    	x=data*w;
    }
    template<typename T> inline void write(T x,char ch='')
    {
    	if(x<0)putchar('-'),x=-x;
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    	if(ch!='')putchar(ch);
    }
    template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
    template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
    template<typename T> inline T min(T x,T y){return x<y?x:y;}
    template<typename T> inline T max(T x,T y){return x>y?x:y;}
    inline void insert(int x,int y,int z)
    {
    	to[++e]=y;
    	nex[e]=beg[x];
    	beg[x]=e;
    	w[e]=z;
    }
    inline void getroot(int x,int f,int ntotal)
    {
    	Mx[x]=0;size[x]=1;
    	for(register int i=beg[x];i;i=nex[i])
    		if(to[i]==f||finish[to[i]])continue;
    		else
    		{
    			getroot(to[i],x,ntotal);
    			size[x]+=size[to[i]];
    			chkmax(Mx[x],size[to[i]]);
    		}
    	chkmax(Mx[x],ntotal-size[x]);
    	if(Mx[x]<Mx[root])root=x;
    }
    inline void cal(int x,int f)
    {
    	dep[x]=dep[f]+1;
    	if(dis[x]<=k)chkmin(ans,dep[x]+Mf[k-dis[x]]);
    	for(register int i=beg[x];i;i=nex[i])
    		if(to[i]==f||finish[to[i]])continue;
    		else dis[to[i]]=dis[x]+w[i],cal(to[i],x);
    }
    inline void add(int x,int f,int v)
    {
    	if(dis[x]<=k)
    	{
    		if(v)chkmin(Mf[dis[x]],dep[x]);
    		else Mf[dis[x]]=inf;
    	}
    	for(register int i=beg[x];i;i=nex[i])
    		if(to[i]==f||finish[to[i]])continue;
    		else add(to[i],x,v);
    }
    inline void solve(int x)
    {
    	finish[x]=1;dep[x]=0;Mf[0]=0;
    	for(register int i=beg[x];i;i=nex[i])
    		if(!finish[to[i]])
    		{
    			dis[to[i]]=w[i];
    			cal(to[i],x);
    			add(to[i],x,1);
    		}
    	for(register int i=beg[x];i;i=nex[i])
    		if(!finish[to[i]])add(to[i],x,0);
    	for(register int i=beg[x];i;i=nex[i])
    		if(!finish[to[i]])
    		{
    			root=0;
    			getroot(to[i],x,size[to[i]]);
    			solve(root);
    		}
    }
    int main()
    {
    	read(n);read(k);
    	for(register int i=1;i<n;++i)
    	{
    		int u,v,w;
    		read(u);read(v);read(w);
    		u++;v++;
    		insert(u,v,w);insert(v,u,w);
    	}
    	Mx[root=0]=inf;
    	getroot(1,0,n);
    	for(register int i=0;i<=k;++i)Mf[i]=inf;
    	solve(root);
    	write(ans==inf?-1:ans,'
    ');
    	return 0;
    }
    
  • 相关阅读:
    github免费私有仓库使用
    空间域平滑滤波器
    Matlab常用函数
    图像处理之图像的平滑与锐化
    Matlab实现直方图均衡化
    matlab图像灰度调整——imadjust函数的使用
    调整图像大小调整图片大小
    Matlab 图像平移、旋转、缩放、镜像
    Matlab注释的几个方法
    训练一个神经网络 能让她认得我
  • 原文地址:https://www.cnblogs.com/hongyj/p/8818284.html
Copyright © 2020-2023  润新知