zoukankan      html  css  js  c++  java
  • [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


    前置知识:点分治

    (k)不大,因此我们开个桶,下标为权值,(h[i])记录距离为(i)的路径最少经过多少条边,然后每次扫一遍即可,记得子树分开考虑

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Fi first
    #define Se second
    #define MK make_pair
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef pair<int,int> pii;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int frd(){
    	int x=0,f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline int read(){
    	int x=0,f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=2e5,M=1e6;
    int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],val[(N<<1)+10];
    int size[N+10],dis[N+10],cnt[M+10],stack[N+10];
    bool vis[N+10];
    pii h[N+10];
    int tot,Max,root,n,K,Ans,top,num;
    void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
    void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
    void Get_root(int x,int fa,int sz){
    	int res=0; size[x]=1;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (son==fa||vis[son])	continue;
    		Get_root(son,x,sz);
    		size[x]+=size[son];
    		res=max(res,size[son]);
    	}
    	res=max(res,sz-size[x]);
    	if (res<Max)	Max=res,root=x;
    }
    void get_dis(int x,int fa,int v){
    	if (dis[x]>K)	return;
    	h[top++]=MK(dis[x],v);
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (son==fa||vis[son])	continue;
    		dis[son]=dis[x]+val[p];
    		get_dis(son,x,v+1);
    	}
    }
    void solve(int x,int v,int c){
    	top=0,dis[x]=v;
    	get_dis(x,0,c);
    }
    void divide(int x){
    	vis[x]=1,cnt[0]=0;
    	stack[num=1]=0;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (vis[son])	continue;
    		solve(son,val[p],1);
    		for (int i=0;i<top;i++)	if (K>=h[i].Fi)	Ans=min(Ans,cnt[K-h[i].Fi]+h[i].Se);
    		for (int i=0;i<top;i++){
    			cnt[h[i].Fi]=min(cnt[h[i].Fi],h[i].Se);
    			stack[++num]=h[i].Fi;
    		}
    	}
    	for (int i=1;i<=num;i++)	cnt[stack[i]]=inf;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (vis[son])	continue;
    		Max=inf,root=0;
    		Get_root(son,x,size[son]);
    		divide(root);
    	}
    }
    int main(){
    	memset(cnt,127,sizeof(cnt)),cnt[0]=0;
    	n=read(),K=read(),Ans=inf;
    	for (int i=1;i<n;i++){
    		int x=read()+1,y=read()+1,z=read();
    		insert(x,y,z);
    	}
    	Max=inf,root=0;
    	Get_root(1,0,n);
    	divide(root);
    	printf(Ans==inf?"-1
    ":"%d
    ",Ans);
    	return 0;
    }
    
  • 相关阅读:
    UWA 技术分享连载 转载
    移动游戏加载性能和内存管理全解析 学习
    英语书籍阅读
    2017年6月
    Unity 官方文档学习
    YAML Class ID Reference
    Unity Blog 学习
    希腊字母
    2017年5月
    转载:书籍
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10213538.html
Copyright © 2011-2022 走看看