http://acm.sgu.ru/problem.php?contest=0&problem=104

水题 DP   但是wa了一次 本来应该初始化为负无穷的 结果初始化为0了 细节呀

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>

#define ll long long

using namespace std;
const int INF=0x3f3f3f3f;
const int N=105;
int v[N][N];
int dp[N][N];
int pre[N][N];
int main()
{
    //freopen("data.in","r",stdin);
    int n,m;
    while(cin>>n>>m)
    {
        memset(pre,0,sizeof(pre));
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        dp[i][j]=-INF;
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        cin>>v[i][j];
        for(int i=1;i<=n;++i)
        for(int j=i;j<=m;++j)
        {
            if(i==1) {dp[i][j]=v[i][j];continue;}
            for(int w=i-1;w<j;++w)
            {
                if(dp[i][j]<dp[i-1][w]+v[i][j])
                {
                    dp[i][j]=dp[i-1][w]+v[i][j];
                    pre[i][j]=w;
                }
            }
        }
        int I=n,J=1;
        for(int j=1;j<=m;++j)
        if(dp[n][j]>dp[n][J])
        J=j;

        cout<<dp[I][J]<<endl;
        stack<int>st;
        while(I>=1)
        {
            st.push(J);
            int l=I,r=J;
            I=l-1;
            J=pre[l][r];
        }
        while(!st.empty())
        {
            cout<<(st.top())<<" ";
            st.pop();
        }cout<<endl;

    }
	return 0;
}

  

posted on 2013-04-19 19:45  夜->  阅读(108)  评论(0编辑  收藏  举报