没有像大背包那样写,但是莫名其妙的过了。
Herbs Gathering
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 781 Accepted Submission(s): 174
Problem Description
Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.
Input
There are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i -th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i -th herb, and the second one is its score.
The total number of different herbs should be no more than 100 . All of the other numbers read in are uniform random and should not be more than 109 .
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i -th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i -th herb, and the second one is its score.
The total number of different herbs should be no more than 100 . All of the other numbers read in are uniform random and should not be more than 109 .
Output
For each test case, output an integer as the maximum sum of scores.
Sample Input
3 70 71 100 69 1 1 2
Sample Output
3
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
struct node
{
long long int v,h;
};
bool cmp(node a,node b)
{
if(a.h==b.h)
return a.v>b.v;
else
return a.h>b.h;
}
node a[110];
long long int n,t;
long long int ans;
void dfs(ll cnt,ll pos,ll lv,ll lt)
{
if(pos>n)
{
if(lt>=0)
ans=max(ans,cnt);
return;
}
if(lt<0)
return;
if(cnt+lv<=ans)
return;
dfs(cnt+a[pos].v,pos+1,lv-a[pos].v,lt-a[pos].h);
dfs(cnt,pos+1,lv-a[pos].v,lt);
}
int main()
{
while(scanf("%I64d%I64d",&n,&t)!=EOF)
{ ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%I64d%I64d",&a[i].h,&a[i].v);
sum+=a[i].v;
}
sort(a+1,a+n+1,cmp);
ans=0;
dfs(0,1,sum,t);
cout<<ans<<endl;
}
return 0;
}
按所需花费的时间排序。然后直接搜 如果搜不到比当前答案大就剪了。。玄学