HDU 5492 Find a path【DP】
来源:互联网 发布:网络消费者品牌形象 编辑:程序博客网 时间:2024/05/22 16:39
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1696 Accepted Submission(s): 736
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手推#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<queue>#include<stack>#include<vector>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))const int M=(30+30-1)*30;const int inf=0x3f3f3f3f;int i,j,k,n,m;int dp[35][35][M+10];int mp[35][35];int main(){ int T; scanf("%d",&T); int cas=1; while(T--){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) for(int j=0;j<m;j++){ scanf("%d",&mp[i][j]); } ms(dp,inf); dp[0][0][mp[0][0]]=mp[0][0]*mp[0][0]; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ for(int k=0;k<=M;k++){ if(dp[i][j][k]!=inf){ if(i+1<n) dp[i+1][j][k+mp[i+1][j]]=min(dp[i+1][j][k+mp[i+1][j]],dp[i][j][k]+mp[i+1][j]*mp[i+1][j]); if(j+1<m) dp[i][j+1][k+mp[i][j+1]]=min(dp[i][j+1][k+mp[i][j+1]],dp[i][j][k]+mp[i][j+1]*mp[i][j+1]); } } } int ans=inf; for(int i=0;i<=M;i++){ if(dp[n-1][m-1][i]!=inf) ans=min(ans,(n+m-1)*dp[n-1][m-1][i]-i*i); } printf("Case #%d: %d\n",cas++,ans); } return 0;}
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 动态规划
- java虚拟机年轻代的gc
- glib使用平衡二叉树实现读配置文件
- UVA-11078 Open Credit System
- 设计模式之---解释器模式
- 什么是渐进增强?
- HDU 5492 Find a path【DP】
- 小菜鸟学习SpringMVC(二) 注解开发
- phalcon中文手册
- 计算机网络基础——基本概念
- 2017年ACM模板(常用)弱渣整理 三、图论
- 毕设DAY1
- CF
- Android facebook VK 登录与分享 开源SDK
- Java 可变参数函数的使用