zoukankan      html  css  js  c++  java
  • loj2024「JLOI / SHOI2016」侦查守卫

    too hard

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int n, d, m, uu, vv, hea[500005], cnt, w[500005], f[500005][22], g[500005][22];
    bool vis[500005];
    struct Edge{
    	int too, nxt;
    }edge[1000005];
    void rn(int &x){
    	char ch=getchar();
    	x = 0;
    	while(ch<'0' || ch>'9')	ch = getchar();
    	while(ch>='0' && ch<='9'){
    		x = x * 10 + ch - '0';
    		ch = getchar();
    	}	
    }
    void add_edge(int fro, int too){
    	edge[++cnt].nxt = hea[fro];
    	edge[cnt].too = too;
    	hea[fro] = cnt;
    }
    void dfs(int x, int af){
    	for(int i=1; i<=d; i++)
    		f[x][i] = w[x];
    	if(vis[x])	f[x][0] = g[x][0] = w[x];
    	f[x][d+1] = 0x3f3f3f3f;
    	for(int ii=hea[x]; ii; ii=edge[ii].nxt){
    		int t=edge[ii].too;
    		if(t!=af){
    			dfs(t, x);
    			for(int i=0; i<=d; i++)
    				f[x][i] = min(f[x][i]+g[t][i], g[x][i+1]+f[t][i+1]);
    			for(int i=d; i>=0; i--)
    				f[x][i] = min(f[x][i], f[x][i+1]);
    			g[x][0] = f[x][0];
    			for(int i=1; i<=d; i++)
    				g[x][i] += g[t][i-1];
    			for(int i=1; i<=d; i++)
    				g[x][i] = min(g[x][i], g[x][i-1]);
    		}
    	}
    }
    int main(){
    	cin>>n>>d;
    	for(int i=1; i<=n; i++)	rn(w[i]);
    	cin>>m;
    	for(int i=1; i<=m; i++){
    		rn(uu);
    		vis[uu] = true;
    	}
    	for(int i=1; i<n; i++){
    		scanf("%d %d", &uu, &vv);
    		add_edge(uu, vv);
    		add_edge(vv, uu);
    	}
    	dfs(1, 0);
    	cout<<g[1][0]<<endl;
    	return 0;
    }
    
  • 相关阅读:
    关于Jquery内存的释放
    jQuery 事件 mouseleave() 方法 mouseenter() 方法
    模版方法模式
    js中return的用法
    HTTP返回码中301与302的区别
    DS介绍
    Java MySql乱码解决
    [IOS] UIViewController的parentViewController属性
    LinuxFind命令
    Linux第一课
  • 原文地址:https://www.cnblogs.com/poorpool/p/8879532.html
Copyright © 2011-2022 走看看