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;
    }
  • 相关阅读:
    Python 里的下划线
    浅谈TCP拆包粘包问题
    40 张图带你搞懂 TCP 和 UDP
    头条面试官问:如何保证网络传输的可靠性?这就很尴尬了
    TCP协议灵魂12问,面试总会用得到(建议收藏)
    TCP网络握手
    HTTP1.0、HTTP1.1和HTTP2.0的区别
    面试官:这波HTTP究极combo,你顶得住吗?_chuhe1989的博客-CSDN博客
    腾讯面试官:说一下Android网络知识和框架?
    网络通信必备基础之Http协议&TCP/IP协议(二)
  • 原文地址:https://www.cnblogs.com/yzm10/p/9545084.html
Copyright © 2011-2022 走看看