Find a path
2016-07-12 15:02
351 查看
Find a path
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
Frog fell into a maze. This maze is a rectangle containing N rows and M columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step, he can go to either the grid right to his current location or the grid below his location. Formally, he can move from grid (x, y) to (x + 1, y) or (x, y +1), if the grid he wants to go exists.
Frog is a perfectionist, so he’d like to find the most beautiful path. He defines the beauty of a path in the following way. Let’s denote the magic values along a path from (1, 1) to (n, m) as A1,A2,…AN+M−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2
In Frog’s opinion, the smaller, the better. A path with smaller beauty value is more beautiful. He asks you to help him find the most beautiful path.
Input
The first line of input contains a number T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤30). Each of the next N lines contains M non-negative integers, indicating the magic values. The magic values are no greater than 30.
Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the minimum beauty value.
Sample Input
1
2 2
1 2
3 4
Sample Output
Case #1: 14
分析:
题意是:有一个N*M的网格,每个格子都有值,问从(1,1)到(N,M)(N+M−1)∑N+M−1i=1(Ai−Aavg)2 的最小值。
设:dp[i][j][k]表示到(i,j)且和为k的∑ni=1(Ai)2的最小值,所以递推公式为:
dp[i][j][k]=min(dp[i-1][j][k-m[i][j]]+m[i][j]*m[i][j],dp[i][j-1][k-m[i][j]]+m[i][j]*m[i][j],dp[i][j][k]);
最后d
[M][i]为到(N,M)的所有的可能的情况,在里面找最小的(N+M-1)*dp
[M][i]-i*i即可。
(感觉是动态规划+背包的思想)
AC代码:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include<limits.h> using namespace std; const int maxn = 40; long long dp[maxn][maxn][1900]; int m[maxn][maxn]; long long min(long long a,long long b) { return a<b?a:b; } int main() { int T,N,M; int cas=0; 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",&m[i][j]); } } for(int i=0;i<=N;i++) { for(int j=0;j<=M;j++) { for(int k=0;k<=1800;k++) { dp[i][j][k]=LLONG_MAX; } } } dp[1][1][m[1][1]]=m[1][1]*m[1][1]; for(int i=1;i<=N;i++) { for(int j=1;j<=M;j++) { for(int k=m[i][j];k<=1800;k++) { if(dp[i-1][j][k-m[i][j]]<LLONG_MAX) dp[i][j][k]=min(dp[i-1][j][k-m[i][j]]+m[i][j]*m[i][j],dp[i][j][k]); if(dp[i][j-1][k-m[i][j]]<LLONG_MAX) dp[i][j][k]=min(dp[i][j-1][k-m[i][j]]+m[i][j]*m[i][j],dp[i][j][k]); } } } long long ans = LLONG_MAX; for(int i=0;i<=1800;i++) { if(dp [M][i]!=LLONG_MAX) ans = min((N+M-1)*dp [M][i]-i*i,ans); } printf("Case #%d: %I64d\n",++cas,ans); } return 0; }
相关文章推荐
- glGenVertexArrays 或glBindVertexArray 暴力访问
- 将R的Shiny App部署到网上遇到的一些坑QAQ
- Java 通过JDBC连接Mysql数据库
- myeclipse中文乱码
- Python内置函数清单
- 数据库自动生成行数
- poj 3264 线段树 入门
- Yelp API(v2) with Mode.js support--handling of authentication
- Yii视图CGridView列表用法实例分析
- Hadoop-2.0命令手册
- wpf 转换器(枚举类型和布尔类型绑定)
- 接口篇(5.2) 04. 调整 DHCP 租约时间 ❀ 飞塔 (Fortinet) 防火墙
- vcf 格式文件详解
- PBOC交易流程图
- 国外SEO分析工具应用心得
- openssh无法登录:server responded "algorithm negotiation failed”
- VirtualBox使用vboxmanage clonehd实现虚拟机克隆
- find -name *lastUpdated -exec rm {} \;
- nginx配置
- Jquery 全选、反选问题解析