HDU-5492 Find a path (枚举+DP)
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.
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.
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; double dp[35][35];
int mp[35][35],n,m; double DP(int eva)
{
double k=n+m-1.0;
dp[n-1][m-1]=(mp[n-1][m-1]-eva/k)*(mp[n-1][m-1]-eva/k);
for(int i=n-2;i>=0;--i)
dp[i][m-1]=(mp[i][m-1]-eva/k)*(mp[i][m-1]-eva/k)+dp[i+1][m-1];
for(int i=m-2;i>=0;--i)
dp[n-1][i]=(mp[n-1][i]-eva/k)*(mp[n-1][i]-eva/k)+dp[n-1][i+1];
for(int i=n-2;i>=0;--i)
for(int j=m-2;j>=0;--j)
dp[i][j]=(mp[i][j]-eva/k)*(mp[i][j]-eva/k)+min(dp[i+1][j],dp[i][j+1]);
return (n+m-1)*dp[0][0];
} int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
scanf("%d",&mp[i][j]); double ans=1e10;
for(int i=0;i<=1770;++i)
ans=min(ans,DP(i));
printf("Case #%d: %.0lf\n",++cas,ans);
}
return 0;
}
HDU-5492 Find a path (枚举+DP)的更多相关文章
- hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online
题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- HDU - 5492 Find a path(方差公式+dp)
Find a path Frog fell into a maze. This maze is a rectangle containing NN rows and MM columns. Each ...
- 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 (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
随机推荐
- HOJ 2252 The Priest(动态规划)
The Priest Source : 计算机学院第二届"光熙杯"程序设计大赛 Time limit : 3 sec Memory limit : 32 M Submitted : ...
- poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions:11148 Accepted: 392 ...
- supervisord部署
https://blog.csdn.net/vbaspdelphi/article/details/54091095https://blog.csdn.net/shudaqi2010/article/ ...
- 【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?
1.背景 最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作.因此编写了自定义的Lint规则,在编写自定义的Lint规则前,当然是需要去把Google的关于Lint检测的 ...
- spring boot 重定向
/** * 测试各个html文件用. * @param model * @return */ @RequestMapping("home") public String home( ...
- POJ2195:Going Home(费用流入门)
http://poj.org/problem?id=2195 #include <iostream> #include <stdio.h> #include <strin ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- 非线性方程(组):一维非线性方程(一)二分法、不动点迭代、牛顿法 [MATLAB]
1. 二分法(Bisection) 1) 原理 [介值定理] 对于连续的一元非线性函数,若其在两个点的取值异号,则在两点间必定存在零点. [迭代流程] 若左右两端取值不同,则取其中点,求其函数值,取中 ...
- SpringData_Repository接口概述
Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 public interface Repository<T, ...
- "字节跳动杯"2018中国大学生程序设计竞赛-女生专场 Solution
A - 口算训练 题意:询问 $[L, R]$区间内 的所有数的乘积是否是D的倍数 思路:考虑分解质因数 显然,一个数$x > \sqrt{x} 的质因子只有一个$ 那么我们考虑将小于$\sqr ...