zoukankan      html  css  js  c++  java
  • HDU 3593(The most powerful force)

    View Code
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define N 10010
    struct edge{
        int to,next;
    }e[N*10];
    int pre[N*10],c[N*10],v[N*10],dp[550][N];
    /*void add(int a,int b)
    {
        e[cnt].to=b;
        e[cnt].next=pre[a];
        pre[a]=cnt++;
    }*/
    void dfs(int u,int g)
    {
        for(int edg=pre[u];edg!=0;edg=e[edg].next)
        {
            int v0=e[edg].to;
            //cout<<v0<<endl;
            if(pre[v0])
            {
                for(int i=0;i<=g-c[v0];i++)
                dp[v0][i]=dp[u][i]+v[v0];
                dfs(v0,g-c[v0]);
                for(int i=c[v0];i<=g;i++)
                if(dp[u][i]<dp[v0][i-c[v0]])dp[u][i]=dp[v0][i-c[v0]];
            }
            else
            {
                for(int i=g;i>=c[v0];i--)
                if(dp[u][i]<dp[u][i-c[v0]]+v[v0])dp[u][i]=dp[u][i-c[v0]]+v[v0];
            }
        }
    }
    int main()
    {
        int n,g;
        while(scanf("%d%d",&n,&g)!=EOF)
        {
            int cnt=1;
            memset(pre,0,sizeof(pre));
            memset(dp,0,sizeof(dp));
            v[0]=c[0]=0;
            for(int i=1;i<=n;i++)
            {
                int f;
                scanf("%d%d%d",&c[i],&v[i],&f);
                if(i!=f)
                {
                    e[cnt].to=i;
                    e[cnt].next=pre[f];
                    pre[f]=cnt++;
                }
                else
                {
                    e[cnt].to=i;
                    e[cnt].next=pre[0];
                    pre[0]=cnt++;
                }
            }
            dfs(0,g);
            //for(int i=0;i<=n;i++,cout<<endl)
            //for(int j=0;j<=g;j++)
            //cout<<dp[i][j]<<" ";
            printf("%d\n",dp[0][g]);
        }
        return 0;
    }
  • 相关阅读:
    1023. 组个最小数
    1021. 个位数统计
    *1020. 月饼
    *1019. 数字黑洞
    1016. 部分A+B
    *1014. 福尔摩斯的约会
    *1013. 数素数
    *1012. 数字分类
    1011. A+B和C
    *1008. 数组元素循环右移问题
  • 原文地址:https://www.cnblogs.com/huangriq/p/2447082.html
Copyright © 2011-2022 走看看