您的位置:首页 > 其它

HDU 1712 ACboy needs your help DP

2015-12-22 19:27 337 查看

题意:有n门课程,m天,给一个矩阵,矩阵每个元素AijA_{ij}花jj天学习ii课程可以得到的价值。

思路:DP:now[ia]=max(now[ia],pre[ia−j]+A[i][j])now[ia] = max(now[ia],pre[ia-j]+A[i][j]),因为Ai1,Ai2,…,AimA_{i1},A_{i2},\ldots,A_{im}是相关的,所以要用一个pre数组来存上一门课程处理过后的状态。

http://acm.hdu.edu.cn/showproblem.php?pid=1712

/*********************************************
Problem : HDU 1712
Author  : NMfloat
InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};
int A[105][105];
int pre[105];
int now[105];

void input() {
rep(i,1,n) rep(j,1,m) scanf("%d",&A[i][j]);
}

void solve() {
cls(now,0); cls(pre,0);
rep(i,1,n) {
rep(j,1,m) {
rrep(ia,j,m) {
now[ia] = max(now[ia],pre[ia-j]+A[i][j]);
}
}
rep(j,1,m) { pre[j] = now[j];}
//puts("");
}
int ans = 0;
rep(i,1,m) {
ans = max(ans,now[i]);
}
printf("%d\n",ans);
}

int main(void) {
//freopen("a.in","r",stdin);
while(scanf("%d %d",&n,&m),n+m) {
input();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: