Find a path

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

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
 
 
  求方格迷宫里的最小方差路径。
  化简式子:    
  

可以看出问题就是要使得所有的(N+M-1)*(A平方的和)减去所有A的和的平方达到最小。

令f[i][j][k]表示走到(i,j)处,且当前走过的格子的法力值的和为k(即SUM{A}=k)的时候的最小的SUM{Ai^2}的值。

最后答案就是MIN{ f[i][j][k]*(N+M-1)-k*k }

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int f[][][];
int e[][];
int main(){
int N,M,T,i,j,k;
cin>>T;
for(int cas=;cas<=T;++cas){
cin>>N>>M;
for(i=;i<=N;++i){
for(j=;j<=M;++j){
cin>>e[i][j];
}
}
memset(f,inf,sizeof(f));
f[][][e[][]]=e[][]*e[][];
for(i=;i<=N;++i){
for(j=;j<=M;++j){
for(k=;k<;++k){
if(f[i][j][k]!=inf){
f[i][j+][k+e[i][j+]]=min(f[i][j+][k+e[i][j+]],f[i][j][k]+e[i][j+]*e[i][j+]);
f[i+][j][k+e[i+][j]]=min(f[i+][j][k+e[i+][j]],f[i][j][k]+e[i+][j]*e[i+][j]);
}
}
}
}
int ans=inf;
for(i=;i<;++i){
if(f[N][M][i]!=inf){
ans=min(ans,f[N][M][i]*(N+M-)-i*i);
}
}
cout<<"Case #"<<cas<<": "<<ans<<endl;
}
return ;
}

hdu-5492-dp的更多相关文章

  1. HDU 5492(DP) Find a path

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...

  2. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  4. 2015合肥网络赛 HDU 5492 Find a path 动归

    HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...

  5. hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online

    题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...

  6. Find a path HDU - 5492 (dp)

    Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

  8. HDU 5492 Find a path

    Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  9. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  10. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 浅谈class私有变量

    class 的前世今生 在 es6 之前,虽然 JS 和 Java 同样都是 OOP (面向对象)语言,但是在 JS 中,只有对象而没有类的概念. 在 JS 中,生成实例对象的传统方法是通过构造函数, ...

  2. yum配合rpm查看软件包安装位置

    今天安装apache,新版本要求除了apache的安装包以外,还要求先安装apr.apr-util和pcre. 开始并没有急着去下载apr的安装包,而是想看看我的操作系统中有没有安装过了这个软件,结果 ...

  3. Jsoup简介

    Jsoup简介 一.概述 Jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来 ...

  4. c++学习之map基本操作

    map作为最常用的数据结构之一,用的好可以大幅度的提升性能. // java_cpp_perftest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h& ...

  5. FTP-IIS Web

    快速搭建一个本地的FTP服务器   如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试. 第一步:配置IIS Web服务器 1.1 控制面板中找到“程序”并打开 1 ...

  6. cogs 444. [HAOI2010]软件安装

    ★★☆   输入文件:install.in   输出文件:install.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述]现在我们的手头有N个软件,对于一个软件i,它要 ...

  7. P4879 ycz的妹子

    思路 让你干啥你就干啥呗 查询第x个妹子就get一下再修改 这里稳一点就维护了三个东西,也许两个也可以 代码 #include <iostream> #include <cstdio ...

  8. POJ3436 ACM Computer Factory(最大流/Dinic)题解

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8944   Accepted: 3 ...

  9. session的理解和使用

    Session的使用与Session的生命周期 1.HttpSession的方法 Object getAttribute(String); Enumeration<String> getA ...

  10. Darknet卷基层浅层特征可视化教程

    目录 Darknet浅层可视化教程 说明 处理步骤 使用python可视化txt文件 Darknet浅层可视化教程 说明 针对YOLO官方提供的c语言版的darknet进行了修改,添加了一些函数,进行 ...