首页 > 代码库 > sgu 104 Little Shop of Flowers
sgu 104 Little Shop of Flowers
经典dp问题,花店橱窗布置,不再多说,上代码
#include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #define N 150 #define inf 0x7f7f7f7f using namespace std; int n, m; int val[N][N], f[N][N]; int fa[N][N]; void print(int now, int place) { if (now == 1) printf("%d ", place); else { print(now-1, fa[now][place]); if (now != n) printf("%d ", place); else printf("%d\n", place); } } int main() { scanf("%d%d", &n, &m); memset(val, 0, sizeof(val)); memset(f, -0x7f, sizeof(f)); for (int i = 0; i <= m; ++i) f[0][i] = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) scanf("%d", &val[i][j]); for (int i = 1; i <= n; ++i) for (int j = i; j <= m-n+i; ++j) for (int k = i-1; k < j; ++k) if (f[i][j] < f[i-1][k] + val[i][j]) { f[i][j] = f[i-1][k] + val[i][j]; fa[i][j] = k; } int ans, place; ans = -inf; for (int i = n; i <= m; ++i) if (ans < f[n][i]) { ans = f[n][i]; place = i; } printf("%d\n", ans); print(n, place); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。