HDU5492 Find a path[DP 方差]

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

2015 ACM/ICPC Asia Regional Hefei Online


题意:(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=35,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x;
}
int T,n,m,l,a[N][N],f[N][N][N*2*30];
int dp(){
    memset(f,127,sizeof(f));
    f[1][1][a[1][1]]=a[1][1]*a[1][1];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            for(int k=0;k<=l*30;k++)
                if(f[i][j][k]<=INF){
                    int x=i+1,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+1;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=0;k<=l*30;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=0;
    while(T--){
        n=read();m=read();l=n+m-1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) a[i][j]=read();
        printf("Case #%d: %d\n",++cas,dp());
    }

    return 0;
}
时间: 2024-08-04 23:34:03

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

hdu5492 find the path dp

这是去年合肥的另一道签到...题目其实很简单...仔细想想范围就能推出来了,第一二维存位置,但显然不够,所以开第三维,由于空间限制只能在和以及平方和二选一,由于平方和太大,所以只能存和.然后dp的值如果存为方差*n的话,显然还需要一维存平方和,但仔细看看式子,如果和以及平方和知道了,也就知道方差了,而方差是很难处理的,事实上,当和固定的时候,平方和越小,方差*n也就越小,这个可以将式子展开得到,所以直接用dp的值来存平方和,先求出和为sum的情况下的最小平方和,然后再枚举sum就可以了. #in

HDU5492 Find a path (dp)

参考:http://blog.csdn.net/u014679804/article/details/48769267   膜拜大神! 题目大意:给N*M(1<=N,M<=30)的矩阵,矩阵的每一格有一个非负权值(<=30) 从(1,1)出发,每次只能向右或向下移动,到达(n,m)时,经过的格子的权值形成序列A, 将式子展开后,化简整理可得:(N+M-1)*s1-s2.其中s1是序列A的平方和,s2是序列A的和的平方. 注意到序列A的和不会超过(30+30-1)*30. 设dp[i][j

HDU 5492 Find a path DP

Find a path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5492 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 cal

CodeForces 407B Long Path (DP)

题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[i]表示从1走到i需要多少步,那么走到房间i+1需要dp[i+1]=dp[i]+1+x+1,这里面第一个1表示走完这步退回到Pi,这个x表示退回到房间Pi再走回来的步数,第二个1表示走完这步到达i+1.我们发现:1.当走到房间Pi的时候,Pi+1到i都是没有走过的即为0次:2.当走到房间i的时候,P

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

http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][x1][y1][x2][y2] 嗯写的时候猜错结论了,本来以为是求下属分配方案中平方和与平均数平方*k的差最小的方案赋给f,没想到是直接找最小的. 代码 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream>

HDU 4865 Peter&#39;s Hobby --概率DP

题意:第i天的天气会一定概率地影响第i+1天的天气,也会一定概率地影响这一天的湿度.概率在表中给出.给出n天的湿度,推测概率最大的这n天的天气. 分析:这是引自机器学习中隐马尔科夫模型的入门模型,其实在这里直接DP就可以了 定义:dp[i][j]为第i天天气为j(0,1,2分别表示三个天气)的概率,path[i][j]记录路径,path[i][j] = k 意思是前一天天气为k时,这一天有最大的概率是天气j. 做一个三重循环,对于每天,枚举今天的天气,再在里面枚举昨天的天气,则有: dp[i][

POJ1787——背包DP(特定状态+回溯)——Charlie&#39;s Change

Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given

poj 2353 双向dp(麻烦的办公室盖章)

题意:给定一个n*m的整数数组.现在要从第一行中的任意点移动到最后一行的任意点,要求每次只能移动一个距离(向左右或者向下,不能向上).求路径所包含数值之和最小的时候的路径. 思路:dp.dp[i][j] 表示从第一行走到到第i行第j列这个位置的最小代价(路径包含的所有数值之和).显然有dp[ i ][ j ] = min(dp[ i ][ j-1 ] , dp[ i ][ j+1 ] , dp[ i-1 ][ j ])+s[ i ][ j ].s[i][j]表示此位置的数值.那么对于每一行,需要

HDU 1074 Doing Homework (状态压缩DP)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3595    Accepted Submission(s): 1424 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lo