Find a path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1536    Accepted Submission(s): 673

Problem Description
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
 
Source

题意:(1,1)到(n,m)向右向下的最小方差路径

把方差的式子化简推理,得到
(n+m-1)*Σai^2-(Σai)^2   i belong path
注意两者平方位置演算时写清了
f[i][j][k]表示到(i,j)和为k的最小平方和,递推就行了
//
// main.cpp
// hdu5492
//
// Created by Candy on 10/2/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
const int N=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int T,n,m,l,a[N][N],f[N][N][N**];
int dp(){
memset(f,,sizeof(f));
f[][][a[][]]=a[][]*a[][];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
for(int k=;k<=l*;k++)
if(f[i][j][k]<=INF){
int x=i+,y=j,w=a[x][y];
f[x][y][k+w]=min(f[x][y][k+w],f[i][j][k]+w*w);
x=i;y=j+;w=a[x][y];
f[x][y][k+w]=min(f[x][y][k+w],f[i][j][k]+w*w);
}
int ans=INF;
for(int k=;k<=l*;k++)
if(f[n][m][k]<INF)
ans=min(ans,l*f[n][m][k]-k*k);
return ans;
}
int main(int argc, const char * argv[]) {
T=read();int cas=;
while(T--){
n=read();m=read();l=n+m-;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) a[i][j]=read();
printf("Case #%d: %d\n",++cas,dp());
} return ;
}
 

HDU5492 Find a path[DP 方差]的更多相关文章

  1. hdu-5492 Find a path(dp)

    题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. 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 ...

  3. POJ 2373 Dividing the Path(DP + 单调队列)

    POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...

  4. [HDU5492]Find a path

    题目大意: 一个n*m的格子,每个格子上都有一个数. 你可以向下或者向右走,从(1,1)走到(n,m),问方差*(n+m-1)最小的路径是哪个? 思路: 方差*(n+m-1)就相当于给格子里每个数乘上 ...

  5. CodeForces 407B Long Path (DP)

    题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...

  6. hdu_2224_The shortest path(dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2224 题意:双调欧几里德旅行商经典问题,找一条最短回路使得该路经过所有的点 题解:dp[i][j]=d ...

  7. Codeforces 408D Long Path (DP)

    题目: One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n ...

  8. NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论

    http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][ ...

  9. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

随机推荐

  1. js时钟&倒计时

    <!DOCTYPE HTML> <html><head><meta charset=UTF-8><title>recursion</t ...

  2. 如何停止CSS3的动画?

    前言 我们在移动端一般使用zepto框架,与其说zepto是jquery的轻量级替代版,不如说是html5替代版我们在js中会用到animate方法执行动画,这个家伙可是真资格的动画,完全是css一点 ...

  3. .NET破解之图片下载器

    自去年五月加入吾爱后,学习了三个月,对逆向破解产生了深厚的兴趣,尤其是对.NET方面的分析:但由于这一年,项目比较忙,事情比较多,破解这方面又停滞了许久,不知道还要好久. 前些天,帮忙批量下载QQ相册 ...

  4. ftp安全设置

    1.文件介绍 /etc/pam.d/vsftpd中ftpuser.user_list文件说明:(在file=后添加改文件路径)/etc/vsftpd.conf中userlist_enable.user ...

  5. Autodesk Cloud Accelerator Program 开始报名

    如果你没有注意到这个消息,那你就会错过一个前往旧金山和硅谷工程师一起工作数周的机会. 摘要一下: 时间: 1月10前提交你的提案,3月飞往旧金山 地点: 旧金山. 包住宿哦~ 不过,既然要去美国,既然 ...

  6. react native 的js 文件从哪里获取

    /** * Loading JavaScript code - uncomment the one you want. * * OPTION 1 * Load from development ser ...

  7. 百度地图SDK 遇到的问题及解决方案

    目前项目工作中用到了百度地图sdk,遇到了不少问题,在此记录一下,顺便吐槽下希望百度能把这地图sdk做的更好用一点. 1,开发环境, Xcode6.0 (6A313) + 百度地图 iOS SDK v ...

  8. 操作系统开发系列—13.c.进程之中断重入

    现在又出现了另外一个的问题,在中断处理过程中是否应该允许下一个中断发生? 让我们修改一下代码,以便让系统可以在时钟中断的处理过程中接受下一个时钟中断.这听起来不是个很好的主意,但是可以借此来做个试验. ...

  9. View的事件体系

    View的滑动 实现手段 优点 缺点 备注 scrollTo/scrollBy 使用简单 只能滑动view的内容,并不会滑动view本身. 且内容超出view本身的布局范围部分的不会显示 不适合有交互 ...

  10. 关于JS变量提升的一些坑

    function log(str) { // 本篇文章所有的打印都将调用此方法 console.log(str); } 函数声明和变量声明总是会被解释器悄悄地被“提升”到方法体的最顶部 变量声明.命名 ...