-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightOj-1047 - Neighbor House.cpp
77 lines (61 loc) · 1.54 KB
/
LightOj-1047 - Neighbor House.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
67
68
69
70
71
72
73
74
75
76
77
/*
UserId: imranRNKENT
Problem: Neighbor House
Language: C++
Catagory: DP
*/
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define maxn 100010
#define ints size_t
#define pbk push_back
#define intl long long
#define mod 1000000007
#define pi 2 * acos(0.0)
#define sf(n) scanf("%d", &n)
#define sd(n) scanf("%lf", &n)
#define sl(n) scanf("%lld", &n)
#define sc(c) scanf(" %c", &c)
#define ss(str) scanf("%s", str)
#define pc(c) printf("%c\n", c)
#define pf(n) printf("%d\n", n)
#define pd(n) printf("%lf\n", n)
#define pl(n) printf("%lld\n", n)
#define ps(str) printf("%s\n", str)
#define ms(n, m) memset(n, m, sizeof(n))
int n, arr[30][3], dp[30][3];
int solve(int i, int j)
{
if(i == n - 1)
return arr[i][j];
if(dp[i][j] != INT_MAX)
return dp[i][j];
if(j == 0)
dp[i][j] = arr[i][j] + min(solve(i + 1, 1), solve(i + 1, 2));
else if(j == 1)
dp[i][j] = arr[i][j] + min(solve(i + 1, 2), solve(i + 1, 0));
else
dp[i][j] = arr[i][j] + min(solve(i + 1, 0), solve(i + 1, 1));
return dp[i][j];
}
int main()
{
int test, cs = 1;
sf(test);
while(test--)
{
for(int i = 0; i < 30; ++i)
dp[i][0] = dp[i][1] = dp[i][2] = INT_MAX;
sf(n);
for(int i = 0; i < n; ++i)
sf(arr[i][0]), sf(arr[i][1]), sf(arr[i][2]);
int ans = min(solve(0, 0), min(solve(0, 1), solve(0, 2)));
printf("Case %d: %d\n", cs++, ans);
}
/*cin.clear();
cin.ignore();
cin.get();*/
return 0;
}