标签:des blog http io ar os for sp strong
Description
Input
Output
Sample Input
Sample Output
#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> #define INF 1000000 using namespace std; int mat[50][50]; int dp[50][50]; int main() { int n,k; int i,j,m; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { dp[i][j]=INF; scanf("%d",&mat[i][j]); } } for(i=1;i<=k;i++) dp[1][i]=0; for(i=2;i<=n;i++) { for(j=1;j<=k;j++) { for(m=1;m<=k;m++) { dp[i][j]=min(dp[i][j],abs(mat[i][j]-mat[i-1][m])+dp[i-1][m]); } } } int ans=dp[n][1]; if(n>1) for(i=2;i<=k;i++) ans=min(ans,dp[n][i]); printf("%d\n",ans); } return 0; }
标签:des blog http io ar os for sp strong
原文地址:http://www.cnblogs.com/sola1994/p/4074274.html