zoukankan      html  css  js  c++  java
  • HDU

    Find a path

    Frog fell into a maze. This maze is a rectangle containing NN rows and MM 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+M1A1,A2,…AN+M−1, and AavgAavg is the average value of all AiAi. The beauty of the path is (N+M1)(N+M–1) multiplies the variance of the values:(N+M1)N+M1i=1(AiAavg)2(N+M−1)∑i=1N+M−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. 

    InputThe first line of input contains a number TT indicating the number of test cases (T50T≤50). 
    Each test case starts with a line containing two integers NN and MM (1N,M301≤N,M≤30). Each of the next NN lines contains MM non-negative integers, indicating the magic values. The magic values are no greater than 30. 
    OutputFor each test case, output a single line consisting of “Case #X: Y”. XX is the test case number starting from 1. YY is the minimum beauty value.Sample Input

    1
    2 2
    1 2
    3 4

    Sample Output

    Case #1: 14




    将公式变形得:(n+m-1)*ΣAi^2-(ΣAi)^2
    dp求出每种和的最小的平方和,最后找出满足公式的最小解。


    #include<bits/stdc++.h>
    #define MAX 31
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    
    int a[MAX][MAX];
    int dp[MAX][MAX][1801];
    
    int main()
    {
        int tt=0,t,n,m,i,j,k;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++){
                for(j=1;j<=m;j++){
                    scanf("%d",&a[i][j]);
                }
            }
            memset(dp,INF,sizeof(dp));
            dp[1][0][0]=0;dp[0][1][0]=0;
            for(i=1;i<=n;i++){
                for(j=1;j<=m;j++){
                    for(k=0;k<=1800;k++){
                        if(k+a[i][j]<=1800) dp[i][j][k+a[i][j]]=min(dp[i][j][k+a[i][j]],min(dp[i-1][j][k],dp[i][j-1][k])+a[i][j]*a[i][j]);
                    }
                }
            }
            int ans=INF;
            for(i=0;i<=1800;i++){
                if(dp[n][m][i]!=INF){
                    ans=min(ans,(n+m-1)*dp[n][m][i]-i*i);
                }
            }
            printf("Case #%d: %d
    ",++tt,ans);
        }
        return 0;
    }
  • 相关阅读:
    Oracle数据库基础select语句用法
    Java中volatile的作用以及用法
    [Java]读取文件方法大全
    经典SQL语句大全
    js动态加载控件jsp页面
    JAVA中List、Map、Set的区别与选用
    表格java代码的相关知识积累
    解决JSP中文乱码问题
    SSH框架的简单学习—Structs学习
    float存储方式编程验证
  • 原文地址:https://www.cnblogs.com/yzm10/p/9545084.html
Copyright © 2011-2022 走看看