首页 > ACM题库 > HDU-杭电 > HDU 4341-Gold miner-动态规划-[解题报告]HOJ
2015
05-23

HDU 4341-Gold miner-动态规划-[解题报告]HOJ

Gold miner

问题描述 :

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.
Capturing a country

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.

输入:

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)

输出:

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)

样例输入:

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

样例输出:

Case 1: 3
Case 2: 7

/*
分析:
    背包小变形,水~、1Y。
    把在同一条射线上的点分类;
    把每一个类当作一组物品、根据到源点的距离递增的顺序对于
同一组的不改变外循环进行dp;
    把不同组的改变外循环dp。
    
                                        2013-07-03
*/

#include"iostream"
#include"cstdio"
#include"cmath"
#include"cstring"
#include"algorithm"
using namespace std;
const int N=205;
const int M=40005;

int n,m,now,pre,dp[2][M];
struct node{
    int x,y,dis2,cost,val;
}E[N];
struct Edge{
    int v,next;
}edge[N];
int tot,head[N];
void add(int a,int b){
    edge[tot].v=b;edge[tot].next=head[a];head[a]=tot++;
}

int cmp(node n1,node n2){
    return n1.dis2<n2.dis2;
}
void build()
{
    int i,l,ff;
    double k1,k2;
    tot=0;
    memset(head,-1,sizeof(head));
    for(i=0;i<n;i++)
    {
        scanf("%d%d%d%d",&E[i].x,&E[i].y,&E[i].cost,&E[i].val);
        E[i].dis2=E[i].x*E[i].x+E[i].y*E[i].y;
    }
    sort(E,E+n,cmp);
    for(i=n-1;i>=0;i--)
    {
        ff=0;
        if(!E[i].x) k1=123456;
        else        k1=1.0*E[i].y/E[i].x;
        for(l=i-1;l>=0;l--)
        {
            if(ff)    break;
            if(!E[l].x) k2=123456;
            else        k2=1.0*E[l].y/E[l].x;
            if(fabs(k1-k2)>1e-5)    continue;
            ff=1;
            add(l,i);
        }
        if(!ff) add(i,i);
    }
}
void dfs(int j,int k,int acc_val,int acc_cost)
{
    int l;
    int sum_val=acc_val+E[k].val;
    int sum_cost=acc_cost+E[k].cost;
    for(l=0;l<sum_cost;l++) dp[now][l]=dp[pre][l];
    for(;l<=m;l++)
    {
        dp[now][l]=dp[pre][l];
        if(dp[now][l] < dp[pre][l-sum_cost]+sum_val)
            dp[now][l]=dp[pre][l-sum_cost]+sum_val;
    }
    j=edge[j].next;
    if(j!=-1)   dfs(j,edge[j].v,sum_val,sum_cost);
}
void DP()
{
    int i,j;
    now=1;pre=0;
    memset(dp,0,sizeof(dp));
    for(i=0;i<n;i++)
    {
        j=head[i];
        if(j==-1) continue;
        if(edge[j].v!=i)  continue;
        now=(now+1)%2;
        pre=1-now;
        dfs(j,edge[j].v,0,0);
    }
}
int main()
{
    int Case=1;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        build();
        DP();
        printf("Case %d: %d\n",Case++,dp[now][m]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/9236951