HDU5492-Find a path
来源:互联网 发布:移动硬盘格式化后数据恢复 编辑:程序博客网 时间:2024/05/22 15:26
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1701 Accepted Submission(s): 738
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) asA1,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.
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
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 integersN 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.
Each test case starts with a line containing two integers
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
12 21 23 4
Sample Output
Case #1: 14
Source
2015 ACM/ICPC Asia Regional Hefei Online
Recommend
wange2014
解题思路:将式子展开后,化简整理可得:(N+M-1)*s1-s2。其中s1是序列A的平方和,s2是序列A的和的平方。因为序列A的和不会超过(30+30-1)*30。设dp[i][j][k]表示到达(i,j),序列和为k时,序列的平方和的最小值。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int a[39][39];int dp[39][39][2000];int n, m;int main(){int t,cas=0;scanf("%d", &t);while (t--){scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)scanf("%d", &a[i][j]);memset(dp, INF, sizeof dp);dp[1][1][a[1][1]] = a[1][1] * a[1][1];for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (i == 1 && j == 1) continue;for (int k = 0; k <= 1900; k++){if (i >= 2 && k >= a[i][j]) dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - a[i][j]] + a[i][j] * a[i][j]);if (j >= 2 && k >= a[i][j]) dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - a[i][j]] + a[i][j] * a[i][j]);}}}int ans = INF;for (int i = 0; i <= 1900; i++){if (dp[n][m][i] == INF) continue;ans =min(ans, (n + m - 1)*dp[n][m][i] - i*i);}printf("Case #%d: %d\n", ++cas, ans);}return 0;}
0 0
- HDU5492-Find a path
- 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 Find a path (动态规划)
- A*Path Find 算法
- Find a path
- Find a path HDU
- HDU 5492 Find a path
- HDU 5492 Find a path
- hdu 5492 Find a path
- HDU 5492 Find a path
- A* search ,find the shortest path
- hdu 5492 Find a path (DP)
- [dp] hdu 5492 Find a path
- hdu 5492 Find a path DP
- 动态规划----背包问题
- Caffe学习:绘制loss和accuracy曲线(使用caffe工具包)
- LVM的工作原理和优缺点
- Android 在桌面显示对话框
- 线性表的顺序存储和链式存储
- HDU5492-Find a path
- Cortex-M的M0,M+,M3,M4,M7几种内核的简单区别
- mac上忘记mysql密码完美解决方案
- 数据结构之链表(C/C++)
- Linux中more和less命令用法
- 前端面试题:从url到页面展现,这之中发生了什么?
- 剑指offer-47.求1+2+3+...+n
- c++运算符重载
- Netty Failure to transfer io.netty:netty-tcnative:jar:${os.detected.classifier}:2.0.0.Final 问题解决