zoukankan      html  css  js  c++  java
  • P2515 [HAOI2010]软件安装

    树形背包

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<vector>
      6 #define MAXN 110
      7 #define MAXM 510
      8 #define pii pair<int,int>
      9 #define rint register int
     10 #define mp make_pair
     11 #define pb push_back
     12 using namespace std;
     13 int read(){
     14     int x=0,f=1;char ch=getchar();
     15     while(ch<'0'||ch>'9'){if('-'==ch)f=-1;ch=getchar();}
     16     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     17     return x*f;    
     18 }
     19 int tot,m;
     20 int C[MAXN],V[MAXN];
     21 vector<int> G[MAXN];
     22 int dfn[MAXN],low[MAXN],sta[MAXN],b[MAXN],top,idx;
     23 int c[MAXN],v[MAXN],cmp[MAXN],n;
     24 int fst[MAXN],nxt[MAXN],from[MAXN],to[MAXN],cnt,pin[MAXN];
     25 void add(int x,int y){
     26     nxt[++cnt]=fst[x],fst[x]=cnt,from[cnt]=x,to[cnt]=y;    
     27     pin[y]++;
     28 }
     29 void tarjan(int x){
     30     dfn[x]=low[x]=(++idx);
     31     sta[++top]=x,b[x]=1;
     32     int y;
     33     for(rint i=0;i<G[x].size();i++){
     34         y=G[x][i];
     35         if(!dfn[y]){
     36             tarjan(y);    
     37             low[x]=min(low[x],low[y]);
     38         }
     39         else if(b[y]){
     40             low[x]=min(low[x],dfn[y]);
     41         }
     42     }
     43     if(dfn[x]==low[x]){
     44         n++;
     45         while(sta[top+1]!=x){
     46             c[n]+=C[sta[top]];
     47             v[n]+=V[sta[top]];
     48             cmp[sta[top]]=n;
     49             b[sta[top]]=0;
     50             top--;    
     51         }
     52     }
     53 }
     54 int f[MAXN][MAXM],dep[MAXN];
     55 int H[MAXM],H1[MAXM],H2[MAXM];
     56 void work(int x,int vl){
     57     if(vl<c[x]){f[x][vl]=0;return;}
     58     vl-=c[x];
     59     memset(H,0,sizeof(H));
     60     memset(H1,0,sizeof(H1));
     61     int y,r=0;
     62     for(rint e=fst[x];e;e=nxt[e]){
     63         y=to[e];
     64         for(rint i=0;i<=vl;i++){
     65             H[i]=max(H[i],H1[i]+f[y][vl-i]);
     66             r=max(r,H[i]);
     67         }
     68         memcpy(H1,H,sizeof(H));
     69     }
     70     f[x][vl+c[x]]=r+v[x];
     71 }
     72 void dp(int x){
     73     b[x]=1;
     74     int y;
     75     for(rint e=fst[x];e;e=nxt[e]){
     76         y=to[e];
     77         if(!b[y]){
     78             dp(y);
     79             for(rint j=m-c[x];j>=0;j--){
     80                 for(rint k=0;k<=j;k++){
     81                     f[x][j]=max(f[x][j],f[x][k]+f[y][j-k]);
     82                 }
     83             }
     84         }
     85     }
     86     for(rint j=m;j>=0;j--){
     87         if(j>=c[x]){
     88             f[x][j]=f[x][j-c[x]]+v[x];
     89         }
     90         else{
     91             f[x][j]=0;
     92         }
     93     }
     94 }
     95 int main()
     96 {
     97   //  freopen("data.in","r",stdin);
     98     tot=read();m=read();
     99     for(rint i=1;i<=tot;i++)C[i]=read();
    100     for(rint i=1;i<=tot;i++)V[i]=read();
    101     int tmp;
    102     for(rint i=1;i<=tot;i++){
    103         tmp=read();
    104         if(tmp)G[tmp].pb(i);
    105     }
    106     for(rint i=1;i<=tot;i++){
    107         if(!dfn[i])tarjan(i);    
    108     }
    109     for(rint i=1;i<=tot;i++){
    110         for(rint j=0;j<G[i].size();j++){
    111             int x=i,y=G[i][j];
    112             if(cmp[x]!=cmp[y]){
    113                 add(cmp[x],cmp[y]);
    114             }
    115         }
    116     }
    117     for(rint i=1;i<=n;i++){
    118         if(!pin[i]){
    119             add(0,i);    
    120         }
    121     }
    122 //    for(rint i=1;i<=cnt;i++){
    123 //        printf("%d %d
    ",from[i],to[i]);    
    124 //    }
    125     memset(b,0,sizeof(b));
    126     dp(0);
    127     printf("%d
    ",f[0][m]);
    128     return 0;    
    129 }            
  • 相关阅读:
    【洛谷P2967】【USACO 2009 Dec】电子游戏 Video Game Troubles
    2021-09-11 刷题 39. 组合总和
    2021-09-10 刷题 160. 相交链表
    2021-09-09 刷题 141. 环形链表
    2021-09-08 刷题 20. 有效的括号
    2021-09-07 刷题 119杨辉三角2
    2021-08-01 刷题 合并两个有序链表
    2021-07-31 leetcode刷题记录 两数之和
    根据需要数据库的内容,封装增删改查的sql函数
    QT 对XML 文件进行增删改查
  • 原文地址:https://www.cnblogs.com/w-h-h/p/8551480.html
Copyright © 2011-2022 走看看