2353

/*
动态规划,如何加速呢》??
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 2000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}


// code begin
int M,N; //100,500
int DP[101][501][2];
int f[101][501];
int stk[50000],top;

int getnext()
{
	char c;
	int ans = 0;
	do
	{
		c = getchar();
	}while( c<'0' || c>'9');
	do
	{
		ans = ans*10+c-'0';
		c = getchar();
	}while( c>='0' && c<='9');
	return ans;
}

int main()
{
	read;
	write;
	while(scanf("%d %d",&M,&N)==2)
	{
		FORi(1,M+1,1)
		{
			FORj(1,N+1,1)
			{
				f[i][j] = getnext();
				//scanf("%d",&f[i][j]);
			}
		}

		memset(DP,0,sizeof(DP));
		FORi(1,M+1,1)
		{
			//左
			FORj(1,N+1,1)
			{
				if(j-1>=1 && DP[i][j-1][0]+f[i][j]<DP[i-1][j][0]+f[i][j])
				{
					DP[i][j][0] = DP[i][j-1][0]+f[i][j];
					DP[i][j][1] = (i-1)*N+j-1-1;
				}
				else
				{
					DP[i][j][0] = DP[i-1][j][0]+f[i][j];
					DP[i][j][1] = (i-2)*N+j-1;
				}
			}

			// 右
			for(int j=N;j>=1;j--)
			{
				if(j+1<=N&&DP[i][j+1][0]+f[i][j]<DP[i-1][j][0]+f[i][j] && DP[i][j+1][0]+f[i][j]<DP[i][j][0])
				{
					DP[i][j][0] = DP[i][j+1][0]+f[i][j];
					DP[i][j][1] = (i-1)*N+j;
				}
				else if( DP[i-1][j][0]+f[i][j]<DP[i][j][0] )
				{
					DP[i][j][0] = DP[i-1][j][0]+f[i][j];
					DP[i][j][1] = (i-2)*N+j-1;
				}
			}
		}
		int Min = INFi,st;
		FORi(1,N+1,1)
		{
			if(DP[M][i][0]<Min)
			{
				Min=DP[M][i][0];
				st = (M-1)*N+i-1;
			}
		}
		
		top = 0;
		do
		{
			stk[top++] = st%N+1;
			//printf("%d\n",st%N+1);
			st = DP[st/N+1][st%N+1][1];
		}while(st>=0);
		for(int j=top-1;j>=0;j--)
			printf("%d\n",stk[j]);
		
	}
	return 0;
}
posted @ 2011-04-09 16:35  AC2012  阅读(181)  评论(0编辑  收藏  举报