题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4502
思路:先按时间排序一下,跑一遍01背包就行,和1881有点像,只不过比那个简单,时间就是一条天然的顺序,dp[j]表示的是最后时间为j能挣到的最多的钱
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; const int maxn = 40000; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; struct node { int s,e,c; friend bool operator < (node a,node b) { return a.e < b.e; } }a[1010]; int dp[110]; int main() { int t; scanf("%d",&t); while(t--) { int m,n; scanf("%d%d",&m,&n); for(int i=1; i<=n; i++) { scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].c); } sort(a+1,a+n+1); int ans = 0; memset(dp,0,sizeof(dp)); for(int i=1; i<=n; i++) { for(int j=m; j>=a[i].e; j--) { dp[j] = max(dp[j],dp[a[i].s-1]+a[i].c); ans = max(ans,dp[j]); } } printf("%d\n",ans); } return 0; }