-
Notifications
You must be signed in to change notification settings - Fork 3
/
lightoj 1047.cpp
66 lines (47 loc) · 995 Bytes
/
lightoj 1047.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
int n;
int a[21][3];
int dp[21][3];
int func(int x,int y)
{
if(x==n)return 0;
if(dp[x][y]!=-1)return dp[x][y];
int re=1e9;
if(y==0){
re=min(re,func(x+1,y+1)+a[x][y]);
re=min(re,func(x+1,y+2)+a[x][y]);
}
else if(y==1)
{
re=min(re,func(x+1,y+1)+a[x][y]);
re=min(re,func(x+1,y-1)+a[x][y]);
}
else
{
re=min(re,func(x+1,y-1)+a[x][y]);
re=min(re,func(x+1,y-2)+a[x][y]);
}
return dp[x][y]=re;
}
int main()
{
int t;
scanf("%d",&t);
for(int ii=1;ii<=t;ii++)
{
memset(dp,-1,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
int re=1e9;
re=min(func(0,0),re);
re=min(func(0,1),re);
re=min(func(0,2),re);
printf("Case %d: %d\n",ii,re);
}
return 0;
}