POJ1015 and uva323 Dynamic planning 01 Backpack Extensions

Source: Internet
Author: User

Solution: At first thought this accord with the optimal sub-structure of the dynamic planning, but later looked at other people's solution found that does not conform to the optimal sub-structure, can not transfer all the state. Because maybe I picked the front one. But if I choose the other one plus the better, this time chose not to choose another because it has disappeared, this time can be done with 01 backpack and a vector array to record the path, with the array of the second dimension 20* M is an absolute difference of 2 sides representing an absolute difference of 1.2 ... 3 ...

This allows each state to be transferred past.

#include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #include <
Cstring> #include <vector> using namespace std;
const int MX = 205;
const int inf = 0X3F3F3F3F;
int DP[MX][4*MX];
vector<int>pre[mx][4*mx];
int DE[MX],SU[MX];
    int main () {int n,m;
    int casei = 1; while (scanf ("%d%d", &n,&m), n| |
        m) {int A, B;
        for (int i = 1, i <= m; i++) for (int j = 1; j < 4*mx; J + +) Pre[i][j].clear ();  for (int i = 1; I <= N;
            i++) {scanf ("%d%d", &a,&b);
            De[i] = a-B;
        Su[i] = a+b;
        } memset (Dp,-1,sizeof (DP));
        int f = 20*m;
        DP[0][F] = 0; for (int i = 1, i <= N; i++) for (int j = m-1; J >= 0; j--) for (int k = 0; k < 2*f; k++ ) if (dp[j][k]!=-1&& (k+de[i]>=0&&k+de[i]<=2*f)) {if (dp[j+ 1][k+de[i]] < dp[J][k]+su[i]) {Dp[j+1][k+de[i]] = dp[j][k]+su[i];
                            Pre[j+1][k+de[i]] = pre[j][k];
                        Pre[j+1][k+de[i]].push_back (i);
        }} int i;
        for (i = 0; Dp[m][f+i] = = -1&&dp[m][f-i] = =-1; i++);
        int temp = dp[m][f+i]>dp[m][f-i]?f+i:f-i;
        int sum1 = (dp[m][temp]+temp-f)/2;
        int sum2 = (dp[m][temp]-temp+f)/2;
        printf ("Jury #%d\n", casei++);
        printf ("Best jury have value%d for prosecution and value%d for defence:\n", sum1,sum2);
        for (int i = 0; i < m; i++) printf ("%d", pre[m][temp][i]);
    printf ("\ n");
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.