标签:
Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2928 Accepted Submission(s): 1441
#include<stdio.h> #include<string.h> #include <iostream> #include <algorithm> #include <stdlib.h> using namespace std; typedef long long LL; const int inf = 999999999; int dp[25][15]; ///dp[i][j]代表打第i层的第j个地鼠所需的最小能量 int a[25][15]; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF){ memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++){ for(int j=1;j<=k;j++){ scanf("%d",&a[i][j]); } } for(int i=1;i<=k;i++) dp[1][i] = 0; for(int i=2;i<=n;i++){ for(int j=1;j<=k;j++) dp[i][j] = inf; } for(int i=2;i<=n;i++){ for(int j=1;j<=k;j++){ ///枚举第i层的地鼠 for(int t=1;t<=k;t++){ ///枚举第i-1层的地鼠 dp[i][j] = min(dp[i][j],dp[i-1][t]+abs(a[i-1][t]-a[i][j])); } } } int MIN = inf; for(int i=1;i<=k;i++){ MIN = min(MIN,dp[n][i]); } printf("%d\n",MIN); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5621442.html