HDU - 5492 Find a path(方差公式+dp)
Find a path
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−1A1,A2,…AN+M−1, and AavgAavg is the average value of all AiAi. The beauty of the path is (N+M–1)(N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2(N+M−1)∑i=1N+M−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.
InputThe first line of input contains a number TT indicating the number of test cases (T≤50T≤50).
Each test case starts with a line containing two integers NN and MM (1≤N,M≤301≤N,M≤30). Each of the next NN lines contains MM non-negative integers, indicating the magic values. The magic values are no greater than 30.
OutputFor each test case, output a single line consisting of “Case #X: Y”. XX is the test case number starting from 1. YY is the minimum beauty value.Sample Input
1
2 2
1 2
3 4
Sample Output
Case #1: 14 将公式变形得:(n+m-1)*ΣAi^2-(ΣAi)^2
dp求出每种和的最小的平方和,最后找出满足公式的最小解。
#include<bits/stdc++.h>
#define MAX 31
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX][MAX];
int dp[MAX][MAX][]; int main()
{
int tt=,t,n,m,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
memset(dp,INF,sizeof(dp));
dp[][][]=;dp[][][]=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
for(k=;k<=;k++){
if(k+a[i][j]<=) dp[i][j][k+a[i][j]]=min(dp[i][j][k+a[i][j]],min(dp[i-][j][k],dp[i][j-][k])+a[i][j]*a[i][j]);
}
}
}
int ans=INF;
for(i=;i<=;i++){
if(dp[n][m][i]!=INF){
ans=min(ans,(n+m-)*dp[n][m][i]-i*i);
}
}
printf("Case #%d: %d\n",++tt,ans);
}
return ;
}
HDU - 5492 Find a path(方差公式+dp)的更多相关文章
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- HDU 5492 Find a path
Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...
- hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online
题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...
- 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
随机推荐
- mybatis 执行查询时报错 【Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: 】
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.sql.SQLE ...
- 【题解】P4799[CEOI2015 Day2]世界冰球锦标赛
[题解][P4799 CEOI2015 Day2]世界冰球锦标赛 发现买票顺序和答案无关,又发现\(n\le40\),又发现从后面往前面买可以通过\(M\)来和从前面往后面买的方案进行联系.可以知道是 ...
- git版本控制-- Windows+Git+TortoiseGit+COPSSH安装图文教程
Windows+Git+TortoiseGit+COPSSH 安装图文教程 教程网址: http://www.liaoxuefeng.com/wiki/0013739516305929606dd183 ...
- wifi androd 整体框架
1. http://blog.csdn.net/myarrow/article/details/8129607/ 2. http://blog.csdn.net/liuhaomatou/articl ...
- java集合讲解干货集
文章都来自网络,收集后便于查阅. 1.Java 集合系列01之 总体框架 2.Java 集合系列02之 Collection架构 3.Java 集合系列03之 ArrayList详细介绍(源码解析)和 ...
- UVA - 10305 【全排列】
题意 要求给出一组 包含 1 - N 的数字的序列 要求这个序列 满足 题给的限制条件 比如 1 2 就是 1 一定要在 2 前面 思路 因为 数据规模较小 可以用 全排列 然后判断每个序列 是否满足 ...
- ASP.NET 4.0 页面 ValidateRequest="false" 失效不起作用
当ASP.NET 2.0升级到 ASP.NET 4.0后,页面的 ValidateRequest="false" 不起作用. 因为 ASP.NET 4.0 请求验证被提前到IHtt ...
- 4.7 希尔(shell)排序法
4-7 ShellSort.c #include <stdio.h> #include "4-1 CreateData.c" //生成随机数的函数 #define AR ...
- python的join()函数
def join(self, iterable): # real signature unknown; restored from __doc__ """ S.join( ...
- ThreadPoolExecutor线程池进阶使用
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...