-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdu4540.cpp
More file actions
48 lines (44 loc) · 724 Bytes
/
Copy pathhdu4540.cpp
File metadata and controls
48 lines (44 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<string.h>
#include <math.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int a[21][11];
int dp[21][11];
int main(){
int n,k,p;
while(cin>>n>>k){
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
cin>>a[i][j];
dp[i][j]=-1;
}
}
int temp;
for(int i=n-2;i>=0;i--){
for(int j=0;j<k;j++){
for(int l=0;l<k;l++){
temp = abs(a[i][j]-a[i+1][l]);
if(dp[i+1][l]==-1){
dp[i+1][l]=0;
}
if(dp[i][j]==-1){
dp[i][j]=dp[i+1][l]+temp;
}else{
dp[i][j]=min(dp[i+1][l]+temp,dp[i][j]);
}
}
}
}
int mi=dp[0][0];
for(int i=1;i<k;i++){
mi=min(dp[0][i],mi);
}
if(n==1){
cout<<0<<endl;
}
else{
cout<<mi<<endl;
}
}
}