Sicily 1342 开心的金明

两种做法,DFS和dp背包。

DFS代码

#include 
using namespace std;
#include 
struct object {
    int price;
    int id;
    bool flag;
};
int n,m;
struct object ob[ 25 ];
struct object sel[ 25 ];
int ctr = 0; 
int max0 = 0;
void solve( int sum, int k ) {
    int i,j;
    int s=0;
//  cout << k << endl;
    if ( sum == n ) {
        s=0;
        for ( i=0;i max0 )
            max0 = s;
    }
    else if ( sum < n ) {
        s=0;
        for ( i=0;i max0 )
            max0 = s;
        
        for ( j=k;j> ob[i].price >> ob[i].id;
            ob[i].flag=true;
        }
        solve(0,0);
        cout << max0 << endl;
    }
    return 0;
}

dp代码

#include 
#include 
int n,m;
struct Object {
    int v,p;
};
Object o[30];
int dp[30001];
int get_max(int a, int b) {
    if (a>b)
        return a;
    else
        return b;
}
int main() {
    int i,j;

    while (scanf("%d",&n)!=EOF) {
        scanf("%d",&m);
        for (i=0;i=o[i].v;j--) {
                dp[j] = get_max(dp[j], dp[j-o[i].v]+o[i].v*o[i].p);
            }
        }
        printf("%d\n", dp[n]);
    }
    return 0;
}                                 


你可能感兴趣的:(Sicily)