Rgb

 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
#include <stdio.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MIN3(a,b,c) MIN(MIN((a),(b)), (c))
int d[21][3], c[21][3];
int calc(int n, int i) {
if(d[n][i]!=-1) return d[n][i];
else {
int r=0;
if(i==0) r=MIN(calc(n-1, 1), calc(n-1, 2)) + c[n][i];
if(i==1) r=MIN(calc(n-1, 0), calc(n-1, 2)) + c[n][i];
if(i==2) r=MIN(calc(n-1, 0), calc(n-1, 1)) + c[n][i];
d[n][i]=r;
return r;
}
return 0;
}
int main() {
freopen("rgb.in", "r", stdin);
freopen("rgb.out", "w", stdout);
int n;
for(int i=0; i<21; i++) for(int j=0; j<3; j++) {d[i][j]=-1; c[i][j]=0;}
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d %d %d", &c[i][0], &c[i][1], &c[i][2]);
d[0][0]=c[0][0];
d[0][1]=c[0][1];
d[0][2]=c[0][2];
int a=calc(n-1, 0);
int b=calc(n-1, 1);
int c=calc(n-1, 2);
int m=MIN3(a,b,c);
printf("%d", m);
return 0;
}