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;}
- HDU5492 十分数学的DP
- hdu5492 dp
- hdu5492(dp)
- hdu5492(递推+数学)
- hdu5492 Find a path dp
- hdu5492 Find a path(DP)
- HDU5492 Find a path DP
- HDU5492 Find a path【DP】
- HDU5492 Find a path (DP+水题)
- [hdu5492]背包问题的扩展
- 十分简单的 dp 最长公共子序列...
- Pro3 十分十分简单的system
- 表现的十分恰到好处
- 十分有用的网址
- hdu5492 2015ACM合肥网赛题 最小方差路径 枚举+简单dp
- 强烈推荐篇将PCA讲解得十分清楚的博文——PCA的数学原理
- 十分
- zoj 3344 //组合数学那种的DP
- UVA(1606)
- javascript高级程序设计第三版 第3章 基本概念 学习笔记
- C3P0数据库连接池配置攻略
- LeetCode 299 Bulls and Cows(公牛和母牛)(HashMap)
- Android UI/UX libraries
- HDU5492 十分数学的DP
- 1051. 复数乘法 (15)
- org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bean class
- Eclipse使用Fresco
- Butter Knife的使用
- 1056. 组合数的和(15)
- ios 网络监测
- jsp网页出现“Cannot return from outside a function or method”
- 【解题报告】Codeforces Round #371 (Div. 2)