zoukankan      html  css  js  c++  java
  • hdu-5492-dp

    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+M1, and Aavg is the average value of all Ai. The beauty of the path is (N+M1) multiplies the variance of the values:(N+M1)N+M1i=1(AiAavg)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 (T50).
    Each test case starts with a line containing two integers N and M (1N,M30). 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 }

      

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define inf 0x3f3f3f3f
     4 int f[35][35][2000]; 
     5 int e[35][35];
     6 int main(){
     7     int N,M,T,i,j,k;
     8     cin>>T;
     9     for(int cas=1;cas<=T;++cas){
    10         cin>>N>>M;
    11         for(i=1;i<=N;++i){
    12             for(j=1;j<=M;++j){
    13                 cin>>e[i][j];
    14             }
    15         }
    16     memset(f,inf,sizeof(f));
    17     f[1][1][e[1][1]]=e[1][1]*e[1][1];
    18     for(i=1;i<=N;++i){
    19         for(j=1;j<=M;++j){
    20             for(k=0;k<2000;++k){
    21                 if(f[i][j][k]!=inf){
    22                     f[i][j+1][k+e[i][j+1]]=min(f[i][j+1][k+e[i][j+1]],f[i][j][k]+e[i][j+1]*e[i][j+1]);
    23                     f[i+1][j][k+e[i+1][j]]=min(f[i+1][j][k+e[i+1][j]],f[i][j][k]+e[i+1][j]*e[i+1][j]);
    24                 }
    25             }
    26         }
    27     }
    28     int ans=inf;
    29     for(i=0;i<2000;++i){
    30         if(f[N][M][i]!=inf){
    31             ans=min(ans,f[N][M][i]*(N+M-1)-i*i);
    32         }
    33     }
    34     cout<<"Case #"<<cas<<": "<<ans<<endl;    
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    Python的命名空间及作用域
    STM32 RTC时钟的配置
    STM32 输入捕获配置
    stm32 看门狗配置
    led灯的驱动电流和电阻
    STM32中TIMx的映射及其通道
    STM32中断定时,控制LED灯
    STM32 用c语言控制4个LED灯从左向右无限流动
    SYSTEMsyssys.c:33:7: error: expected '(' before 'void' __ASM void MSR_MSP(u32 addr)
    STM32 PWM输出(映射)
  • 原文地址:https://www.cnblogs.com/zzqc/p/9301823.html
Copyright © 2011-2022 走看看