*背包(1)可多取分组

Gold miner

Time Limit: 2000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
Homelesser likes playing Gold miners in class. He has to pay much attention to the teacher to avoid being noticed. So he always lose the game. After losing many times, he wants your help.

To make it easy, the gold becomes a point (with the area of 0). You are given each gold's position, the time spent to get this gold, and the value of this gold. Maybe some pieces of gold are co-line, you can only get these pieces in order. You can assume it can turn to any direction immediately.
Please help Homelesser get the maximum value.
 

Input

There are multiple cases.
In each case, the first line contains two integers N (the number of pieces of gold), T (the total time). (0<N≤200, 0≤T≤40000)
In each of the next N lines, there four integers x, y (the position of the gold), t (the time to get this gold), v (the value of this gold). (0≤|x|≤200, 0<y≤200,0<t≤200, 0≤v≤200)
 

Output

Print the case number and the maximum value for each test case.
 

Sample Input

3 10
1 1 1 1
2 2 2 2
1 3 15 9
3 10
1 1 13 1
2 2 2 2
1 3 4 7

Sample Output

Case 1: 3
Case 2: 7
#include<cstdio>
#include<algorithm>
#include<cstring>
const int maxn = 205;
using namespace std;
struct node{
    int x,y,t,v;
}a[maxn];
int b[maxn][maxn];
int c[maxn];
int g[maxn*maxn];
bool cmp(node a,node b){
    if(a.x*b.y == b.x*a.y)
        return a.y < b.y;
    return a.y*b.x < a.x*b.y;
}
int main(){
    int n,m;
    int cas = 0;
    while(~scanf("%d%d",&n,&m)){
        for(int i = 0; i < n; i++)
            scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].t,&a[i].v);
        sort(a,a+n,cmp);
        int i,j;
        int cnt1 = 0;
        for( i = 0; i < n; i = j){
            int cnt2 = 0;
            b[cnt1][cnt2++] = i;
            int sum_t = a[i].t;
            int sum_v = a[i].v;
            for( j = i+1; j < n; j++){
                if(a[j].x*a[i].y == a[j].y*a[i].x){
                    b[cnt1][cnt2++] = j;
                    sum_t += a[j].t;
                    sum_v += a[j].v;
                    a[j].t = sum_t;
                    a[j].v = sum_v;
                }
                else break;
            }
            c[cnt1] = cnt2;
            cnt1++;
        }
        memset(g,0,sizeof(g));
        for(int i = 0; i < cnt1; i++){
            for(int t = m; t >= 0; t--){
                for(int k = 0; k < c[i]; k++){
                    if(t - a[b[i][k]].t >= 0)
                        g[t] = max(g[t],g[t-a[b[i][k]].t] + a[b[i][k]].v);
                }
            }
        }
        printf("Case %d: %d\n",++cas,g[m]);
    }
    return 0;
}

 

posted @ 2015-08-27 19:38  Tobu  阅读(128)  评论(0)    收藏  举报