HDU5492 Find a path (动态规划)
来源:互联网 发布:大数据主要来源于哪些 编辑:程序博客网 时间:2024/05/22 17:19
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1866 Accepted Submission(s): 809
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
题意:走矩阵,每次只能往下或者往右,从坐上到右下。求方差。
方差有个公式是,D(x)=(E(x))^2-E(x^2)
直接dp,加上一维限制其中一个变量即可。
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;const int INF=1e9+7;const int MAXN=35;const int N=2000;int dp[MAXN][MAXN][N];int a[MAXN][MAXN];int b[MAXN][MAXN];int main(){ int t; scanf("%d",&t); int cas=1; 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]); b[i][j]=a[i][j]*a[i][j]; for(int k=0;k<N;k++) dp[i][j][k]=INF; } } dp[1][1][a[1][1]]=b[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<N;k++){ if(i>1&&k>=a[i][j]&&dp[i-1][j][k-a[i][j]]!=INF){ dp[i][j][k]=min(dp[i-1][j][k-a[i][j]]+b[i][j],dp[i][j][k]); } if(j>1&&k>=a[i][j]&&dp[i][j-1][k-a[i][j]]!=INF){ dp[i][j][k]=min(dp[i][j-1][k-a[i][j]]+b[i][j],dp[i][j][k]); } } } } int res=INF; for(int k=0;k<N;k++){ if(dp[n][m][k]!=INF){ res=min(res,(n+m-1)*dp[n][m][k]-k*k); } } printf("Case #%d: %d\n",cas++,res); }}
阅读全文
0 0
- HDU5492 Find a path (动态规划)
- 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+水题)
- hdu 5492 Find a path 动态规划
- 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)
- hdu 5492 Find a path(dp)
- HDU 5492 Find a path(DP)
- HDU 5492 Find a path(DP)
- 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
- git学习3
- 合法IP地址的判断、关于给定字符串,求最大不重复子串 -- JAVA 算法题
- 【快搭】springboot+jsp
- (二)UI Automator测试项目的建立
- Java爬虫--页面跳转爬数据
- HDU5492 Find a path (动态规划)
- MySQL查询表格内容基本语句1
- kafka 安装和使用测试(mac版)
- PAT (Basic Level) Practise (中文)1010. 一元多项式求导 (25)
- Hadoop之MapReduce调度:通过shell进行多日期的串行跑批统计
- json版的轮播图
- Html中行内元素有哪些?块级元素有哪些?
- socket与http的区别
- ACM_51nod 1354 选数字