HDU-4616 Game 树形DP

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4616

  比较典型的树形DP题目,f[u][j][k]表示以点u为子树,经过 j 个陷阱的最大值,其中k=0表示从u点出发,k=1表示终点为点u。则转移方程为:f[u][j+is_rtap][k]=Max{ f[v][j][k] | v为u的儿子节点,0<=j<=m }。要分别对每颗子树求出最大值,枚举其中的两颗子树,一颗出,一颗进,更新最大值。要注意在更新k=0时的最大值是,j要从1开始遍历,因为如果当前u节点存在trap,而u中的其中一个子节点v没有trap,那么会使f[u][1][0]加上f[v][0][0]的值,而不满足题目中遇到满足的m后停止下来。

  1 //STATUS:C++_AC_93MS_5540KB
  2 #include 
  3 #include 
  4 #include 
  5 //#include 
  6 #include 
  7 #include 
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include <string>
 14 #include 
 15 #include 
 16 #include 
 17 #include 
 18 #include 
 19 #include 
 20 #include 
 21 #include <set>
 22 #include 
 23 #pragma comment(linker,"/STACK:102400000,102400000")
 24 using namespace std;
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=50010,M=2000010;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=100000,STA=8000010;
 39 const LL LNF=1LL<<60;
 40 const double EPS=1e-8;
 41 const double OO=1e15;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return aa:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 
 58 struct Edge{
 59     int u,v;
 60 }e[N<<1];
 61 int first[N],next[N<<1],tr[N];
 62 int T,n,m,mt;
 63 LL f[N][4][2],w[N],ans;
 64 
 65 void adde(int a,int b)
 66 {
 67     e[mt].u=a,e[mt].v=b;
 68     next[mt]=first[a],first[a]=mt++;
 69     e[mt].u=b,e[mt].v=a;
 70     next[mt]=first[b],first[b]=mt++;
 71 }
 72 
 73 void dfs(int u,int fa)
 74 {
 75     int i,j,k,v;
 76     for(i=first[u];~i;i=next[i]){
 77         v=e[i].v;
 78         if(v==fa)continue;
 79         dfs(v,u);
 80         for(j=0;j<=m;j++){
 81             for(k=0;k+j<=m;k++){
 82                 if(j1]+f[v][k][0]);
 83                 if(k0]+f[v][k][1]);
 84                 if(j+k1]+f[v][k][1]);
 85             }
 86         }
 87         for(j=0;j+tr[u]<=m;j++){
 88             if(j && tr[u]0]=Max(f[u][j+tr[u]][0],f[v][j][0]+w[u]);
 89             if(j1]=Max(f[u][j+tr[u]][1],f[v][j][1]+w[u]);
 90         }
 91     }
 92 }
 93 
 94 int main()
 95 {
 96  //   freopen("in.txt","r",stdin);
 97     int i,j,a,b;
 98     scanf("%d",&T);
 99     while(T--)
100     {
101         scanf("%d%d",&n,&m);
102         mem(f,0);
103         for(i=0;i){
104             scanf("%I64d%d",&w[i],&tr[i]);
105             f[i][tr[i]][0]=f[i][tr[i]][1]=w[i];
106         }
107         mt=0;mem(first,-1);
108         for(i=1;i){
109             scanf("%d%d",&a,&b);
110             adde(a,b);
111         }
112 
113         ans=0;
114         dfs(0,-1);
115 
116         printf("%I64d\n",ans);
117     }
118     return 0;
119 }

 

转载于:https://www.cnblogs.com/zhsl/p/3221495.html

你可能感兴趣的:(HDU-4616 Game 树形DP)