zoj 3703(背包)

简单的背包问题, 算是一种多重的吧。

解题的关键在于,要控制最后所用的时间最少,所以在程序的最开始应该先将 输入的各种题目 以时间升序排列, 然后就可以保证每次都以时间小的优先选, 这样就可以保证最后相同的吸引值和解题数的情况下所话的时间最少。

 

Happy Programming Contest

Time Limit: 2 Seconds      Memory Limit: 65536 KB

In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved. Since the girl would prefer pink balloon rather than black balloon, each color is assigned a value to measure its attractiveness. Usually, the boy is good at programming while the girl is charming. The boy wishes to solve problems as many as possible. However, the girl cares more about the lovely balloons. Of course, the boy's primary goal is to make the girl happy rather than win a prize in the contest.

Suppose for each problem, the boy already knows how much time he needs to solve it. Please help him make a plan to solve these problems in strategic order so that he can maximize the total attractiveness value of balloons they get before the contest ends. Under this condition, he wants to solve problems as many as possible. If there are many ways to achieve this goal, he needs to minimize the total penalty time. The penalty time of a problem is equal to the submission time of the correct solution. We assume that the boy is so clever that he always submit the correct solution.

Input

The first line of input is an integer N (N < 50) indicating the number of test cases. For each case, first there is a line containing 2 integers T (T <= 1000) and n (n <= 50) indicating the contest length and the number of problems. The next line contains n integers and the i-th integer ti (ti <= 1000) represents the time needed to solve the ith problem. Finally, there is another line containing n integers and the i-th integer vi (vi <= 1000) represents the attractiveness value of the i-th problem. Time is measured in minutes.

Output

For each case, output a single line containing 3 integers in this order: the total attractiveness value, the number of problems solved, the total penalty time. The 3 integers should be separated by a space.

Sample Input

2
300 10
10 10 10 10 10 10 10 10 10 10
1 2 3 4 5 6 7 8 9 10
300 10
301 301 301 301 301 301 301 301 301 301
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000

Sample Output

55 10 550
0 0 0

Author: HUANG, Qiao
Contest: The 13th Zhejiang University Programming Contest

 

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

struct node
{
    int key,time,cnt;
    int ty;
}dp[1100][1100];

struct node1
{
    int w,key;
}g[1100];

int t,n;

int cmp(node1 t,node1 t1)
{
    return t.w < t1.w;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&t,&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&g[i].w);
        for(int i=1;i<=n;i++)
            scanf("%d",&g[i].key);
        sort(g+1,g+1+n,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=t;j++)
            {
                dp[i][j] = dp[i-1][j];
                //dp[i][j].ty=j;
                if(j<g[i].w) continue;
                int flag=0;
                int tkey,tcnt,time;
                tkey = dp[i-1][ j-g[i].w ].key + g[i].key;
                tcnt = dp[i-1][ j-g[i].w ].cnt+1;


                if( tkey < dp[i][j].key) continue;
                if( tkey>dp[i][j].key )
                {
                    dp[i][j].cnt=tcnt;
                    dp[i][j].key=tkey;
                    dp[i][j].ty=j-g[i].w;
                    dp[i][j].time=dp[i-1][j-g[i].w].time+j;
                    continue;
                } // 如果解题数都相同的话
                if(tcnt < dp[i][j].cnt) continue;
                if(tcnt>dp[i][j].cnt)
                {
                    dp[i][j].cnt=tcnt;
                    dp[i][j].ty=j-g[i].w;
                    dp[i][j].time=dp[i-1][j-g[i].w].time+j;
                    continue;
                }
            }
        }
        int mx=0,mxcnt=0,mxi=0;
        for(int i=0;i<=t;i++)
        {
            if(dp[n][i].key<mx) continue;
            if(dp[n][i].key > mx) 
            {
                mx=dp[n][i].key;
                mxcnt=dp[n][i].cnt;
                mxi=dp[n][i].time;
                continue;
            }
            if(dp[n][i].cnt<mxcnt) continue;
            if(dp[n][i].cnt>mxcnt) 
            {
                mxcnt=dp[n][i].cnt;
                mxi=dp[n][i].time;
                continue;
            }
            if(dp[n][i].time<mxi) mxi=dp[n][i].time;
        }
        /*int time=0;
        int tg[1100];
        for(int i=1;i<=t;i++)
        {
            if(dp[n][i].key==mx&&dp[n][i].cnt==mxcnt)
            {
                int tx=n,ty=i,tc=dp[n][i].cnt;
                while(1)
                {
                    if(dp[tx-1][ dp[tx][ty].ty ].cnt!=tc)
                    {
                        tg[tc]=ty-dp[tx][ty].ty;
                        tc--;
                        ty = dp[tx][ty].ty;
                    }
                    if(tc==0) break;
                    tx--;
                }
                sort(tg+1,tg+mxcnt+1);
                time=0;
                int tt=0;
                for(int j=1;j<=mxcnt;j++)
                {
                    time+=tt+tg[j];
                    tt+=tg[j];
                }
                if(time<mxi) mxi=time;
            }
        }*/
        printf("%d %d %d\n",mx,mxcnt,mxi);
    }
    return 0;
}

 

 

 

posted @ 2013-04-22 10:57  chenhuan001  阅读(353)  评论(0编辑  收藏  举报