hdu 5492 (매트릭스 모델 의 변형)

1189 단어 dp
참고 블 로그:http://blog.csdn.net/u014679804/article/details/48769267
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 35;
const int inf = 0x3f3f3f3f;
int n,m,map[maxn][maxn];
int dp[maxn][maxn][1800];

int main()
{
	int t,cas = 1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				scanf("%d",&map[i][j]);
		memset(dp,inf,sizeof(dp));
		dp[1][1][map[1][1]] = map[1][1] * map[1][1];
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				for(int k = 0; k <= 1800; k++)
				{
					if(dp[i][j][k] == inf) continue;
					if(i + 1 <= n)
						dp[i+1][j][k+map[i+1][j]] = min(dp[i+1][j][k+map[i+1][j]],dp[i][j][k] + map[i+1][j]*map[i+1][j]);
					if(j + 1 <= m)
						dp[i][j+1][k+map[i][j+1]] = min(dp[i][j+1][k+map[i][j+1]],dp[i][j][k] + map[i][j+1]*map[i][j+1]);
				}
		int ans = inf;
		for(int i = 0; i <= 1800; i++)
			if(dp[n][m][i] != inf)
				ans = min(ans,(n+m-1)*dp[n][m][i] - i*i);
		printf("Case #%d: %d
",cas++,ans); } return 0; }

좋은 웹페이지 즐겨찾기