zoukankan      html  css  js  c++  java
  • hdu 5492 Find a path dp

    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

    求从(1,1)到(n,m)的最小方差路径
    化简方差的式子可以得到(n+m-1)*Σai^2-(Σai)^2
    结合题目数据可以得到Σai的值最多不超过(30+30-1)*30
    所以我们可以枚举Σai的值
    dp所有情况,dp[i][j][k]表示到(i,j)和为k的最小平方和

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<cmath>
    #include<queue>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    int mapn[35][35], dp[35][35][1810];
    int main() {
        std::ios::sync_with_stdio(false);
        int T, cnt = 0;
        cin >> T;
        while( T -- ) {
            cnt ++;
            int n, m;
            cin >> n >> m;
            for( int i = 0; i < n; i ++ ) {
                for( int j = 0; j < m; j ++ ) {
                    cin >> mapn[i][j];
                }
            }
            cout << "Case #" << cnt << ": ";
            for( int i = 0; i < 35; i ++ ) { //记得初始化
                for( int j = 0; j < 35; j ++ ) {
                    for( int k= 0; k < 1810; k ++ ) {
                        dp[i][j][k] = 1e9;
                    }
                }
            }
            dp[0][0][mapn[0][0]] = mapn[0][0] * mapn[0][0];
            for( int i = 0; i < n; i ++ ) {
                for( int j = 0; j < m; j ++ ) {
                    if( i + 1 < n ) {
                        for( int k = 0; k <= 59*30; k ++ ) {
                            if( dp[i][j][k] != 1e9 ) {
                                dp[i+1][j][k+mapn[i+1][j]] =
                    min( dp[i+1][j][k+mapn[i+1][j]], dp[i][j][k]+mapn[i+1][j]*mapn[i+1][j]);
                            }
                        }
                    }
                    if( j + 1 < m ) {
                        for( int k = 0; k <= 59*30; k ++ ) {
                            if( dp[i][j][k] != 1e9 ) {
                                dp[i][j+1][k+mapn[i][j+1]] =
                    min( dp[i][j+1][k+mapn[i][j+1]], dp[i][j][k]+mapn[i][j+1]*mapn[i][j+1]);
                            }
                        }
                    }
                }
            }
            int sum = 1e9;
            for( int i = 0; i <= 59*30; i ++ ) {
                if( dp[n-1][m-1][i] != 1e9 ) {
                    sum = min( sum, (n+m-1)*dp[n-1][m-1][i]-i*i );
                }
            }
            cout << sum << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    算法练习:求字符串的最长重复子串(Java实现)
    Oracle数据库中遇到的坑
    解决Oracle死锁问题步骤
    转:Spring Cache抽象详解
    Spring MVC测试框架详解——服务端测试
    转:SpringMVC中日期格式的转换
    freemarker判断是否为空
    jQuery Pagination分页插件
    Java链式方法
    mysql强制索引和禁止某个索引
  • 原文地址:https://www.cnblogs.com/l609929321/p/9013628.html
Copyright © 2011-2022 走看看