码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5492(DP) Find a path

时间:2015-10-05 21:55:03      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492

题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是这些数的方差最小。

Find a path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 961    Accepted Submission(s): 431


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) as A1,A2,AN+M1, and Aavg is the average value of all Ai. The beauty of the path is (N+M1) multiplies the variance of the values:(N+M1)N+M1i=1(AiAavg)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 (T50).
Each test case starts with a line containing two integers N and M (1N,M30). 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
技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #define inf 0x3f3f3f3f
 4 using namespace std;
 5 
 6 int mi(int x,int y)
 7 {
 8     if (x<y)
 9     return x;
10     else
11     return y;
12 }
13  int ans,t,n,m,dp[35][35][1805],a[35][35];
14 int main()
15 {
16    
17     cin>>t;
18     for (int cas=1;cas<=t;cas++)
19     {
20         cin>>n>>m;
21         for (int i=1;i<=n;i++)
22         for (int j=1;j<=m;j++)
23         cin>>a[i][j];
24         
25         memset(dp,inf,sizeof(dp));
26         dp[0][1][0]=0;
27         dp[1][0][0]=0;
28         
29         for (int i=1;i<=n;i++)
30         {
31             for (int j=1;j<=m;j++)
32             {
33                 for (int k=0;k<=1800;k++)
34                 {
35                     if (dp[i-1][j][k]!=inf)
36                     {
37                            dp[i][j][k+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i-1][j][k]+a[i][j]*a[i][j]);
38                     } 
39                     if (dp[i][j-1][k]!=inf)
40                     {
41                            dp[i][j][k+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i][j-1][k]+a[i][j]*a[i][j]);
42                     }
43                 }
44             }
45         }
46         ans=inf;
47         for (int i=0;i<=1800;i++)
48         {
49             if (dp[n][m][i]!=inf)
50             ans=mi(ans,dp[n][m][i]*(n+m-1)-i*i);
51         }
52         cout <<"Case #"<<cas<<": "<<ans<<endl;
53     }
54     return 0;
55 }
View Code

 

 

HDU 5492(DP) Find a path

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4856285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!