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;
    }
  • 相关阅读:
    Node.js入门学习笔记
    Memcached服务器安装、配置、使用详解
    基于Dubbo框架构建分布式服务
    Apache Beam:一个开源的统一的分布式数据处理编程库
    Spring Cloud Netflix构建微服务入门实践
    内部排序算法:快速排序
    内部排序算法:冒泡排序
    内部排序算法:基数排序
    Java常见面试题
    svn+ssh方式svn服务器和客户端的配置[转载]
  • 原文地址:https://www.cnblogs.com/yzm10/p/9545084.html
Copyright © 2011-2022 走看看