Hdu 5492 Find a path【Dp】
来源:互联网 发布:如何查看tcp端口 编辑:程序博客网 时间:2024/06/05 05:41
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1759 Accepted Submission(s): 770
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
题目大意:
给你一个N*M的矩阵,我们从左上角走到右下角,走到的位子上的数字必须拿取,而且每一步只能向右走或者向下走。
让你求公式的最小值。
思路:
我们分解开公式,不难化简式子变成:(n+m-1)*ΣAi^2-(ΣAi)^2;
那么我们利用背包思想,我们设定ΣAi为背包容量,我们希望价值最小,所以我们希望ΣAi^2越小,所以我们设定Dp【i】【j】【k】表示到点(i,j)处,ΣAi的值为k能够获得的最小的ΣAi^2的值。
那么其状态转移方程不难写出:
然后最终暴力枚举所有情况维护最小值即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[45][45];int dp[35][35][60*32*2];int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { int n,m;scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); for(int k=0;k<=(n+m)*60;k++) { dp[i][j][k]=0x3f3f3f3f; } } } 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++) { for(int k=0;k<=(n+m)*60;k++) { if(i==1&&j==1)continue; if(i-1>=1&&k-a[i][j]>=0&&dp[i-1][j][k-a[i][j]]!=0x3f3f3f3f)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-1>=1&&k-a[i][j]>=0&&dp[i][j-1][k-a[i][j]]!=0x3f3f3f3f)dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k-a[i][j]]+a[i][j]*a[i][j]); } } } int output=0x3f3f3f3f; for(int k=0;k<=(n+m)*60;k++) { if(dp[n][m][k]!=0x3f3f3f3f) { output=min(output,(n+m-1)*(dp[n][m][k])-k*k); } } printf("Case #%d: %d\n",++kase,output); }}
(N+M−1)∑N+M−1i=1(Ai−Aavg)2
阅读全文
0 0
- hdu 5492 Find a path (DP)
- [dp] hdu 5492 Find a path
- hdu 5492 Find a path DP
- hdu 5492 Find a path【dp】
- hdu 5492 Find a path(dp)
- HDU 5492 Find a path(DP)
- HDU 5492 Find a path(DP)
- HDU 5492 Find a path【DP】
- Hdu 5492 Find a path【Dp】
- HDU 5492 Find a path(数学 + DP)
- hdu 5492 Find a path(公式推导,DP)
- HDU 5492 Find a path
- HDU 5492 Find a path
- hdu 5492 Find a path
- HDU 5492 Find a path
- DP HDOJ 5492 Find a path
- Find a path HDU
- hdu 5492 Find a path 动态规划
- 函数式编程
- 链表创建及反转
- json
- wpf 如何对window样式中的close按钮添加关闭事件
- spring-mybatis 之SqlSessionFactoryBean
- Hdu 5492 Find a path【Dp】
- 简单网络管理协议(SNMP)通过第三方API SNMP++ 提取进程名称,路径与PID信息
- android Pernission的所有权限说明
- 常见网页错误400/401/403/404/500网页错误代码表示的含义
- kali2.0配置msf连接postgresql数据库
- C++11有关线程同步的使用
- [Android]广播机制概述 一
- TensorFlow之Keras, TensorLayer, Tflearn库比较【转】
- Lamamda和Linq的一些基本用法