junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Gold miner

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2240    Accepted Submission(s): 855


Problem Description
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
 

Author
HIT
 

Source


题意:玩家在(0,0)位置,给N块金块的横纵坐标,消耗时间,价值,求在t时间内能得到的最大值,其中在一条直线上的金块需要将前面的挖走才能挖后面的。 
思路:将斜率相同的金块归为一类,将同一类前面的金块的价值和耗时依次加到后面的金块上,然后用分组背包解法即可,即每类取一个金块,分组背包dp[i][j]表示前i组物品在j容量内得到的最大值,dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[k]] + v[k]),其中c为代价,v为价值,k为第i组的物品,该方程降维处理代码如下
# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
struct node
{
    int x, y, t, v;
}a[205];
int b[205][205], dp[40010];
int cmp(node a, node b)
{
    if(a.x*b.y != a.y*b.x)
        return a.y*b.x < b.y*a.x;
    else
        return a.y < b.y;
}
int main()
{
    int n, t, cas=1;
    while(~scanf("%d%d",&n,&t))
    {
        memset(dp, 0, sizeof(dp));
        memset(b, 0, sizeof(b));
        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 cnt=0;
        for(int i=0; i<n; ++i)
        {
            b[cnt][++b[cnt][0]] = i;
            if(a[i].x*a[i+1].y == a[i].y*a[i+1].x)
            {
                a[i+1].t += a[i].t;
                a[i+1].v += a[i].v;
                if(i == n-1)
                    ++cnt;
            }
            else
                ++cnt;
        }
        for(int i=0; i<cnt; ++i)
            for(int j=t; j>=0; --j)
                for(int k=1; k<=b[i][0]; ++k)
                    if(j>=a[b[i][k]].t)
                        dp[j] = max(dp[j], dp[j-a[b[i][k]].t] + a[b[i][k]].v);
        printf("Case %d: %d\n",cas++,dp[t]);
    }
    return 0;
}


posted on 2017-02-01 00:50  junior19  阅读(123)  评论(0编辑  收藏  举报