HDU3466背包01

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3126    Accepted Submission(s): 1288

Problem Description
Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
If he had M units of money, what’s the maximum value iSea could get?
 
Input
There are several test cases in the input.

Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.

The input terminates by end of file marker.

 
Output
For each test case, output one integer, indicating maximum value iSea could get.
 
Sample Input
2 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3
 
Sample Output
5 11
 
 
 
 
 
 
 
 
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[5005];
int p[505],q[505],c[505];
int n,m;
int pd1(int p[],int q[],int c[])
{
    int temp;
    for(int i=1;i<=n-1;i++)
        for(int j=i+1;j<=n;j++)
    {
        if(q[i]-p[i]>q[j]-p[j])
        {
            temp=p[i];
            p[i]=p[j];
            p[j]=temp;
            temp=q[i];
            q[i]=q[j];
            q[j]=temp;
            temp=c[i];
            c[i]=c[j];
            c[j]=temp;
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(p,0,sizeof(p));
        memset(q,0,sizeof(q));
        memset(c,0,sizeof(c));
        memset(dp,0,sizeof(dp));
        for(int k=1;k<=n;k++)
            scanf("%d%d%d",&p[k],&q[k],&c[k]);
            pd1(p,q,c);
            for(int i=1;i<=n;i++)
              for(int j=m;j>=q[i];j--)
                dp[j]=max(dp[j],dp[j-p[i]]+c[i]);
              printf("%d\n",dp[m]);
    }
    return 0;
}
上一篇:C++中 string 的用法大全


下一篇:29. 栈的push,pop序列