您的位置:首页 > 其它

zoj 3211 Dream City 动态规划

2011-11-05 18:17 375 查看
原题连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3211

这个是我看到的很经典的动态规划问题,状态转移很经典啊

解题思路: 一颗斜率(b)小的树,如果这次不用,那么他再也不会用到了。我是这么理解的……这样的话,就产生了一个顺序,也就是可以dp的顺序

我表达的不好,如果还是不懂,看代码吧,看完了就懂了

个人感觉很经典,至少状态很难想

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

int n,m;
struct note
{
int a,b;
}tree[300];

int dp[300][300];
bool cmp(const note a,const note b)
{
return a.b < b.b;
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n >> m;
for(int i = 1;i <= n;i++)
cin >> tree[i].a;
for(int i = 1;i <= n;i++)
cin >> tree[i].b;
sort(tree+1,tree+n+1,cmp);
//cout << tree[0].b << tree[1].b;

memset(dp,0,sizeof(dp));
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
dp[i][j] = max(dp[i-1][j],dp[i-1][j-1] + tree[i].a + tree[i].b*(j-1));
}
}
cout << dp
[m] << "\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tree