HDU 5492 Find a path

解决一个迷宫问题,寻找从起点到终点路径中方差最小的路径。采用动态规划方法,通过记录每个点上的累计和与累计平方和来计算路径的方差。

Find a path

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 5492
64-bit integer IO format: %I64d      Java class name: Main

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 $A_1,A_2,…A_{N+M−1}$, and $A_{avg}$ is the average value of all $A_i$. The beauty of the path is (N+M–1) multiplies the variance of the values:$(N+M−1)\sum_{i=1}^{N+M−1}(A_i−A_{avg})^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\leq 50).$
Each test case starts with a line containing two integers N and M $(1\leq N,M\leq 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


Sample Output
Case #1: 14


Source
2015 ACM/ICPC Asia Regional Hefei Online

解题:动态规划,最小方差路

$dp[i][j][k]表示在(i,j)格子中\sum{A_i}为k的时候最小的\sum{A_i^2}$

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 35;
 5 int mp[maxn][maxn],dp[maxn][maxn][1810];
 6 int main(){
 7     int kase,n,m,cs = 1;
 8     scanf("%d",&kase);
 9     while(kase--){
10         scanf("%d%d",&n,&m);
11         for(int i = 1; i <= n; ++i)
12             for(int j = 1; j <= m; ++j)
13                 scanf("%d",mp[i] + j);
14         memset(dp,0x3f,sizeof dp);
15         dp[1][0][0] = dp[0][1][0] = 0;
16         for(int i = 1; i <= n; ++i){
17             for(int j = 1; j <= m; ++j){
18                 for(int k = 0; k <= 1800; ++k){
19                     if(dp[i-1][j][k] != INF)
20                         dp[i][j][k + mp[i][j]] = min(dp[i][j][k + mp[i][j]],dp[i-1][j][k] + mp[i][j]*mp[i][j]);
21                     if(dp[i][j-1][k] != INF)
22                         dp[i][j][k + mp[i][j]] = min(dp[i][j][k + mp[i][j]],dp[i][j-1][k] + mp[i][j]*mp[i][j]);
23                 }
24             }
25         }
26         int ret = INF;
27         for(int i = 0; i <= 1800; ++i)
28             if(dp[n][m][i] < INF) ret = min(ret,(n + m - 1)*dp[n][m][i] - i*i);
29         printf("Case #%d: %d\n",cs++,ret);
30     }
31     return 0;
32 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4868265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值