dp (1)

 

D - Tree of Tree ZOJ - 3201 

Dp I started this topic is so defined [i] [j] [0] dp [i] [j] [1] means that for the i-th node also select 0 for the j not choose this node, 1 for election this node.

Then I wrote on the subject has gone wrong to write out a wrong understanding, and understanding the problem correctly found written like this trouble. Difficult to write the transfer equation.

Internet search solution to a problem, dp online so basically defined [i] [j] represent the i-th selected nodes of the tree nodes is a maximum weighted value of j.

So this can become a tree dp + 01 backpack.

The state transition equation should be the dp [u] [j] = max (dp [u] [j], dp [u] [jk] + dp [v] [k]) Actually, I think this transfer equation is not as good miss you.

The 01 backpack is the number of nodes in the enumeration to take u to the root of the tree of every single sub-tree.

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 10;
int dp[200][200];
vector<int>G[200];
int n, k;

void dfs(int u,int pre)
{
    for (int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if (v == pre) continue;
        dfs(v, u);
        for (int j = k; j >= 1; j--)
        {
            for (int h = 0; h < j; h++)
            {
                dp[u][j] = max(dp[u][j], dp[u][j - h] + dp[v][h]);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i <= n; i++) G[i].clear();
        for (int i = 0; i < n; i++) {
            scanf("%d", &dp[i][1]);
        }
        for(int i=1;i<n;i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(0, -1);
        int ans = 0;
        for(int i=0;i<n;i++)
        {
            ans = max(ans, dp[i][k]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
Tree dp

 

 

C - Brackets POJ - 2955 

In fact, I feel like this subject that is seeking palindrome, so much like the rabbit and the title, but it still has a little bit different

Recall rabbit topics  rabbit portal

 

State transition equation that is the subject

if(条件判断) dp[i][j]=max(dp[i+1][dp[j-1]+2,dp[i][j])

else dp[i][j]=max(dp[i+1][j],dp[i][j-1])

There is a place on this topic, because we defined dp [i] [j] represents the maximum number of matches from i to j, so, () () ()

This situation, if you value is a state transition, it will be a problem, and

Because dp [1] [2] = 2 dp [2] [3] = 1 dp [3] [4] = 2 so dp [1] [4] = dp [2] [3] +2 This is not right this time, we should find a cut off point.

dp [1] [4] The cutting point is dp [1] [2] + dp [3] [4] = 4

So there is also a for loop to find the break, I thought to understand this is to look at solutions to problems.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
int dp[200][200];
char s[200];

int main()
{
    while(scanf("%s",s+1)!=EOF)
    {
        if (s[1] == 'e') break;
        memset(dp, 0, sizeof(dp));
        int len = strlen(s + 1);
        int ans = 1;
        for(int i=2;i<=len;i++)
        {
            for(int j=1;j+i-1<=len;j++)
            {
                int ends = i + j - 1;
                if ((s[j]=='('&&s[ends]==')')||(s[j]=='['&&s[ends]==']')) dp[j][ends] =dp[j + 1][ends - 1] + 2;
                else dp[j][ends] = max(dp[j + 1][ends], dp[j][ends - 1]);
                for (int k = j; k < ends; k++) dp[j][ends] = max(dp[j][ends], dp[j][k] + dp[k+1][ends]);
                //printf("j %c ends %c dp[%d][%d]=%d\n", s[j],s[ends],j, ends, dp[j][ends]);
            }
        }
        printf("%d\n", dp[1][len]);
    }
    return 0;
}
Interval dp

 

Q - maximum reimbursement HDU - 1864 

I feel this topic is still relatively simple, ah, string processing is to pay attention to it, you can look at my other blog post sscanf method to handle very convenient.

https://www.cnblogs.com/EchoZQN/p/10830015.html

After processing 01 backpack is a simple, important to note that because there is a decimal, so 100 * converted to an integer can be handled.

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 3e6 + 10;
int dp[maxn], n, m;
int a[100];
double q;

int main()
{
    while(scanf("%lf%d",&q,&m)!=EOF)
    {
        if (m == 0) break;
        n = (int)(q * 100);
        int tot = 0;
        for(int k=1;k<=m;k++)
        {
            int num;
            scanf("%d", &num);
            char ch, cs[110];
            double mon;
            int A = 0, B = 0, C = 0, flag = 0;
            for(int i=1;i<=num;i++)
            {
                scanf("%s", cs);
                sscanf(cs, "%c:%lf", &ch, &mon);
                int mm = (int)(mon * 100);
                if (ch == 'A') A += mm;
                else if (ch == 'B') B += mm;
                else if (ch == 'C') C += mm;
                else flag = 1;
                if (A > 60000 || B > 60000 || C > 60000 || (A + B + C) > 100000) flag = 1;
            //    printf("%d %d %d\n", A, B, C);
            }
            if (flag == 0) a[++tot] = A + B + C;
        }
        memset(dp, -inf, sizeof(dp));
        dp[0] = 0;
        //for (int i = 1; i <= tot; i++) printf("%d\n", a[i]);
        //printf("n=%d\n", n);
        for(int i=1;i<=tot;i++)
        {
            for(int j=n;j>=a[i];j--)
            {
                dp[j] = max(dp[j], dp[j - a[i]]);
            }
        }
        //printf("%d\n", dp[n]);
        int ans = 0;
        for(int i=n;i>=0;i--)
        {
            if(dp[i]>=0)
            {
                ans = i;
                break;
            }
        }
        printf("%.2lf\n", ans*1.0 / 100);
    }
    return 0;
}
01 backpack

 

This is today's dp training, the next step is to review the previous algorithms.

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/10994493.html
DP