Gold miner
Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 705Accepted Submission(s): 300
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
Source
Recommend
zhuyuanchen520
题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v。而且有的金子可能在一条直线上,那只能先抓近的,再抓远的。求在给定时间T下,所能获得的最大价值。
这题可以转化为分组的背包问题。分组的背包问题详解见背包九讲。
先将所有点按照斜率排序,斜率相同按照距离排序。
然后进行分组,将斜率相同的分进同一个组。
比如有5个点1,2,3,4,5,6.
1斜率小,2,3斜率相同,4,5,6斜率相同。那分三组(1),(2,3),(4,5,6)
然后在同一个组内需要处理下。比如(2,3)是先要抓2才能抓3的。那就把2,3的t和v加起来给3。这样2,3就只能取一个了,就变成分组的背包问题了。
#include <stdio.h>
#include <algorithm>
#include<math.h>
#include <string.h>
using namespace std;
struct st
{
int x;
int y;
int v;
int w;
}no[300],u[300][300];
int cmp(const void *a,const void *b)
{
double k1,k2;
k1=(double)(*(struct st *)a).x/(*(struct st *)a).y;
k2=(double)(*(struct st *)b).x/(*(struct st *)b).y;
if(fabs(k1-k2)>0.000001) return k1>k2?1:-1;//先把斜率 按从小到大排列
// if(k1==k2) return k1>k2?1:-1;//先把斜率 按从小到大排列 注意对于浮点型的二级排序要特别注意 千万不要直接比较是否相等
else return (*(struct st *)a).y>(*(struct st *)b).y?1:-1;//从小到大
}
int shu[300];
int dp[50009];
int mm=1;
int main()
{
int i,j,m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&no[i].x,&no[i].y,&no[i].w,&no[i].v);
}
//sort(no,no+m,cmp);
qsort(no,m,sizeof(no[0]),cmp);
int cos=0;
memset(shu,0,sizeof(shu));
for(i=0;i<m;i++)
{
u[cos][shu[cos]].x=no[i].x;
u[cos][shu[cos]].y=no[i].y;
u[cos][shu[cos]].w=no[i].w;
u[cos][shu[cos]].v=no[i].v;
shu[cos]++;
if(i!=m-1)
{
while(i!=m-1&&(double)no[i+1].x/no[i+1].y==(double)no[i].x/no[i].y)
{
u[cos][shu[cos]].x=no[i+1].x;
u[cos][shu[cos]].y=no[i+1].y;
u[cos][shu[cos]].w=u[cos][shu[cos]-1].w+no[i+1].w;
u[cos][shu[cos]].v=u[cos][shu[cos]-1].v+no[i+1].v;
shu[cos]++;
i++;
}
cos++;
}
}
int k;
memset(dp,0,sizeof(dp));
for(i=0;i<=cos;i++)
{
for(j=n;j>=0;j--)
{
for(k=0;k<shu[i];k++)
{
// if(j-u[i][k].w>=0&&dp[j]<dp[j-u[i][k].w]+u[i][k].v)
if(j-u[i][k].w>=0)
dp[j]=dp[j]>dp[j-u[i][k].w]+u[i][k].v?dp[j]:dp[j-u[i][k].w]+u[i][k].v;
}
}
}
printf("Case %d: ",mm++);
printf("%d\n",dp[n]);
}
return 0;
}
分享到:
相关推荐
hdu题目分类
HDU图论题目分类
杭电ACM课件2014版之 (HDUACM201303版_07)背包专题
ACM HDU题目分类,我自己总结的大概只有十来个吧
关于hdu的动态规划的题目,包括一些水题,还有一些经典的动态规划题目。
HDU 动态规划(46道题目
本人杭电上题目java实现的代码,绝无其他无用的内容,知道的都晓得这是什么
其中有pku、hdu、zoj等各大oj的题目分类,pku的oj比较详细一点,按不同算法进行分类,均系从网上自行搜集的。
这是 HDU ACM 部分题目的源代码,后续会继续上传。
hdu 一些简单题目 ac代码 大概100道
hdu杭电所有题目按照ac数量排序,python分析
100道 acm C语言 hdu 解题报告
里面是本人在HDU上做的600多道题目的CODE,给大家分享一下
自己做的HDU ACM已经AC的题目
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码