标签:des style blog java color strong
Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1521 Accepted Submission(s): 748
水DP,dp[i][j]表示第i时刻打第j只地鼠所消耗的最小能量,最小能量应该是上一时刻某位置与该位置的绝对值的差+上一位置所消耗的能量值,对上一位置都求一遍取最小值
对应方程为dp[i][j] = min(dp[i][j],dp[i-1][k]+abs(ma[i][j]-ma[i-1][k]))初始化的话dp[i][j]第一行初始为0其余为INF。
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; #define N 505 #define INF 0x3f3f3f3f int ma[21][11],dp[21][11]; int main() { int n,m,i,j,k,x,minx; while(~scanf("%d %d",&n,&m)) { for(i = 0;i<n;i++) for(j = 0 ; j < m ; j++) { scanf("%d",&ma[i][j]); dp[i][j] = INF; } for(i = 0 ; i < m ; i++)dp[0][i] = 0; for(i = 1 ; i < n ; i++) { for(j = 0 ; j < m ; j++) { for(k = 0 ; k < m ; k++) dp[i][j]= min(dp[i][j],abs(ma[i][j]-ma[i-1][k])+dp[i-1][k]); } } minx = INF; for(j = 0 ; j <m ; j++) minx = min(minx,dp[n-1][j]); printf("%d\n",minx); } return 0 }
标签:des style blog java color strong
原文地址:http://www.cnblogs.com/llei1573/p/3847687.html