题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881
3 6 3 3 3 2 2 4 1 3 4 5 1 1 10 2 3 6 1 2 3 1 1 -1
7 16
代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
int n, ans;
struct bg
{
int h, l, t;
}a[32];
bool cmp(bg a, bg b)
{
return a.t < b.t;
}
void dfs(int i, int hh, int tt)
{
if(i == n)
{
if(hh > ans)
ans = hh;
return;
}
if(tt+a[i].l <= a[i].t)
{
dfs(i+1,hh+a[i].h,a[i].l+tt);
}
dfs(i+1,hh,tt);
return;
}
int main()
{
while(cin>>n)
{
if(n < 0)
break;
ans = 0;
for(int i = 0; i < n; i++)
{
cin>>a[i].h>>a[i].l>>a[i].t;
}
sort(a,a+n,cmp);
dfs(0,0,0);
cout<<ans<<endl;
}
return 0;
}
hdu1881 毕业bg(深搜dfs),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/37912749