您的位置:首页 > 其它

oldssoj2668Bombing plan(树形dp)

2015-08-27 21:59 323 查看

题目描述

Kingdom Y is in the war with kingdom X. Kingdom X consists of N cities,there are N-1 bidirectional roads which are all 1 long ,each of them connect a pair of cities,the N cities are all connect by the N-1 bidirectional.People can travel through the roads.

Now kingdom Y is going to bomb kingdom X. Every city of kingdom X has its own value W. If city i was to be bombed, then all the cities that lie within the distance W(i) from city i would be destroyed as well. The king of kingdom Y wants to know the minimum
bombing time that can destroy all the cities in kingdom X. Could you help him?

目前,WBS正打算攻击地球。地球上的每个城市有自己的权值W。如果一个城市被放上炸弹,那么所有距离该城市距离小于W[i]的城市都将被破坏。WBS想要知道,最少需要多少个炸弹,使得地球上的所有城市都被破坏。

输入

There are multiple test cases. Please process till EOF.

In each test case:

First line: an integer n(n<=10^5) indicating the number of city

Second line:contain n numbers w[i](0<=w[i]<=100) ,indicating that the value of city[i],

Next n - 1 lines: each contains two numbers ui and vi, (1 ≤ ui,vi<=n), indicates that there’s one road connecting city ui and vi.

输入文件有多组测试数据,输入文件以EOF结尾。
对于每组测试数据,数据的第一行包括一个整数n(n<=10^5),表示城市数量。

接下来的第二行,包括n个正数W[i] (0<=w[i]<=100),表示城市i的权值。

输出

For each case,output one number, denotes the minimum number of bombing times.
对于每一组测试数据,输出一个整数表示最小需要的炸弹数量。

样例输入

51 1 1 1 11 22 33 44 5

样例输出

2

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=100005;
const int inf=maxn*3;
struct data{
int v,nxt;
}e[maxn*2];
int n,fst[maxn],w[maxn],num[maxn],cnt,m,sum,ans;
int g[maxn][105],f[maxn][105];
int gg[maxn][105],ff[maxn][105];
inline int get(){
char c;while(!isdigit(c=getchar()));
int v=c-48;while(isdigit(c=getchar()))v=v*10+c-48;
return v;
}
inline void add(int x,int y){
e[++cnt].v=y;e[cnt].nxt=fst[x];fst[x]=cnt;++num[x];
}
inline void dfs(int x,int fa){
for(int i=fst[x];i;i=e[i].nxt){
int y=e[i].v;
if(y!=fa) dfs(y,x);
}
if(num[x]==1){
g[x][0]=0;
f[x][w[x]]=1;
return;
}
for(int i=fst[x];i;i=e[i].nxt){
int y=e[i].v;
if(y==fa)continue;
ff[y][0]=f[y][0];
gg[y][0]=g[y][0];
for(int j=1;j<=m;++j){
ff[y][j]=min(ff[y][j-1],f[y][j]);
gg[y][j]=min(gg[y][j-1],g[y][j]);
}
}
for(int j=0;j<=m;++j){
for(int i=fst[x];i;i=e[i].nxt){
int k=e[i].v;sum=0;
if(k==fa)continue;
for(int t=fst[x];t;t=e[t].nxt){
int son=e[t].v;
if(son==fa || son==k)continue;
if(j==0){
sum+=ff[son][j+1];
continue;
}
if(son!=k && son!=fa){
sum+=min(ff[son][j+1],gg[son][j-1]);
}
}
f[x][j]=min(f[x][j],f[k][j+1]+sum);
}
}
sum=0;
for(int i=fst[x];i;i=e[i].nxt){
int son=e[i].v;
if(son==fa)continue;
sum+=f[son][0];
}
g[x][0]=min(g[x][0],sum);
for(int j=1;j<=m;++j){
for(int i=fst[x];i;i=e[i].nxt){
int k=e[i].v;if(k==fa)continue;
sum=0;
for(int t=fst[x];t;t=e[t].nxt){
int son=e[t].v;
if(son!=k && son!=fa){
sum+=min(ff[son][j],gg[son][j-1]);
}
}
g[x][j]=min(g[x][j],g[k][j-1]+sum);
}
}
sum=0;
for(int i=fst[x];i;i=e[i].nxt){
int son=e[i].v;if(son==fa)continue;
sum+=min(ff[son][w[x]+1],gg[son][w[x]-1]);
}
f[x][w[x]]=min(f[x][w[x]],1+sum);
return;
}
inline void Mem(){
for(int i=0;i<=n+3;++i)
for(int j=0;j<105;++j){
f[i][j]=g[i][j]=ff[i][j]=gg[i][j]=inf;
}
}
int main(){
while(scanf("%d",&n)==1){
Mem();
memset(num,0,sizeof(num));memset(fst,0,sizeof(fst));cnt=0;m=0;
ans=inf;
for(int i=1;i<=n;++i){
w[i]=get();
if(w[i]>m)m=w[i];
}
++m;
for(int i=1;i<n;++i){
int x,y;
x=get();y=get();
add(x,y);add(y,x);
}
add(1,0);
dfs(1,0);
for(int i=0;i<=m;++i)ans=min(f[1][i],ans);
printf("%d\n",ans);
}
}


思想:这是一个树形dp;f[ i ][ j ]为以i为树根的树全炸掉且向上炸j个结点的最少炸弹数;g[ i ][ j ]为以i为树根的树未全炸掉,且距i结点最远的未炸掉结点不超过j;

状态转移方程为:

不炸i结点:

f[ i ][ j ]=f[ k ][ j+1 ]+∑(min(min(f[ son ][ 0 ]……f[ son ][ j+1 ]),min(g[ son ][ 0 ]……g[ son ][ j-1 ])));

g[ i ][ j ]=g[ k ][ j-1 ]+∑(min(min(f[ son ][ 0 ]……f[ son ][ j ]),min(g[ son ][ 0 ]……g[ son ][ j-1 ])));

其中j==0的情况要特殊处理一下;

炸i结点:

f[ i ][ w[ i ] ]=1+∑min(min(f[ son ][ 0 ]……f[ son ][ w[i]+1 ]),min(g[ son ][ 0 ]……g[ son ][ w[ i ]-1 ]));

其中min()的数可以用数组ff,gg预处理;

注意:inf不能取太大,否则会超

评价:一题不错的树形dp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: