HDU5492 十分数学的DP

来源:互联网 发布:heinonline数据库 编辑:程序博客网 时间:2024/05/22 17:45

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+M−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=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.

Input
The first line of input contains a number T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤30). 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

这题我一开始就是不知道到底怎么个转移法。。
真的不知道
我甚至想出一种转移路径的方式
想过两遍dp
但是时间爆炸…

最后突然发现他的结果用的不是浮点型。。
这就很因垂死听了…
有平均数怎么可能没有浮点呢…
全拆了之后发现了乱七八糟的分母抵消掉了
就留了一坨整数、、
这才做了出来

#include<iostream>#include<algorithm>#include<memory.h>using namespace std;int dp[31][31][1900];int tu[31][31];int n, m;int inf = 1900*31*31;int main(){    int T;    cin >> T;    int u = 0;    while (T--)    {        cin >> n >> m;        memset(tu, 0, sizeof(tu));        for (int a = 1;a <= n;a++)        {            for (int b = 1;b <= m;b++)            {                cin >> tu[a][b];                for (int c = 0;c <= 1899;c++)                {                    dp[a][b][c] = inf;                }            }        }        dp[1][1][tu[1][1]] = tu[1][1] * tu[1][1];        for (int a = 1;a <= n;a++)        {            if (a < n)            {                for (int b = 1;b <= m;b++)                {                    for (int c = 0;c <= 1800;c++)                    {                        if (b < m)                        {                            if(dp[a][b][c]!=inf)dp[a][b+1][c + tu[a][b+1]] = min(dp[a][b][c] + tu[a][b+1] * tu[a][b+1], dp[a][b+1][c + tu[a][b+1]]);                        }                        if (dp[a][b][c] != inf)dp[a + 1][b][c + tu[a + 1][b]] = min(dp[a][b][c] + tu[a + 1][b] * tu[a + 1][b],dp[a+1][b][c + tu[a + 1][b]]);                    }                }            }            else            {                for (int b = 1;b <= m;b++)                {                    for (int c = 0;c <= 1800;c++)                    {                        if (b < m)                        {                            if (dp[a][b][c] != inf)dp[a][b + 1][c + tu[a][b + 1]] = min(dp[a][b][c] + tu[a][b + 1] * tu[a][b + 1], dp[a][b + 1][c + tu[a][b + 1]]);                        }                    }                }            }        }        int sum = inf;        for (int a = 0;a <= 1770;a++)        {            if(dp[n][m][a]!= inf)            sum = min(sum, (n+m-1)*dp[n][m][a]-a*a);        }        printf("Case #%d: %d\n", ++u, sum);    }    return 0;}
0 0
原创粉丝点击