3466 Proud Merchants

Proud Merchants

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


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
 
很明显是背包问题,然后就开始做了,但一直结果都不对,弄得我都怀疑我01背包程序写错了。最后看解题报告知道还要根据Q-P排序,至于原因就是鹏哥说的 
f[j]=max(f[j],f[j-p[i]]+v[i[) ,动态规划是局部最优推向全局最优的,要使f[j]最优,f[j-p[i[]]得最优,j最小是Q[i],所以让f[Q[i]-P[i]]最优,所以要把Q[i]-P[i]最小的推出来
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int P[500+5],Q[500+5],v[500+5];
int dp[5000+5];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d%d%d",&P[i],&Q[i],&v[i]);
        memset(dp,0,sizeof(dp));
         //cout<<m<<" "<<Q[1]<<endl;

         for(int i=1;i<=n-1;i++)
            for(int j=1;j<=n-1-i;j++)
            if(Q[j]-P[j]>Q[j+1]-P[j+1])
            {
                int p1=P[j];
                P[j]=P[j+1];
                P[j+1]=p1;

                int q=Q[j];
                Q[j]=Q[j+1];
                Q[j+1]=q;

                int v1=v[j];
                v[j]=v[j+1];
                v[j+1]=v1;
            }
          //  for(int i=1;i<=n;i++)
             //   cout<<P[i]<<" "<<Q[i]<<" "<<v[i]<<endl;

        for(int j=1;j<=n;j++)
        {

            for(int i=m;i>=P[j];i--)
            {
                if(i>=Q[j])
                 dp[i]=max(dp[i],dp[i-P[j]]+v[j]);
                // cout<<dp[i]<<" "<<endl;

           }
         //  for(int k=1;k<=m;k++)  cout<<dp[k]<<" ";
         //  cout<<endl;
        }
  //  for(int i=1;i<=m;i++)
   //     cout<<dp[i]<<" ";
        printf("%d\n",dp[m]);

    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值