码迷,mamicode.com
首页 > 其他好文 > 详细

sgu 104 Little Shop of Flowers

时间:2014-05-19 22:26:02      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   java   

    经典dp问题,花店橱窗布置,不再多说,上代码

bubuko.com,布布扣
#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);
}
bubuko.com,布布扣

 

sgu 104 Little Shop of Flowers,布布扣,bubuko.com

sgu 104 Little Shop of Flowers

标签:style   blog   class   c   code   java   

原文地址:http://www.cnblogs.com/handsomeJian/p/3731388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!