-
Notifications
You must be signed in to change notification settings - Fork 0
/
1047.cpp
102 lines (88 loc) · 2.61 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Problem: 1047 - Neighbor House
// Contest: LightOJ
// URL: http://lightoj.com/volume_showproblem.php?problem=1047
// Memory Limit: 32 MB
// Time Limit: 500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
//#pragma GCC diagnostic ignored "-Wformat"
using namespace std ;
#define Pi 2*acos(0.0)
#define LINF 1LL<<50
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define MAX 500006
#define MAX1 10000008
#define mem(a,v) memset(a,v,sizeof(a))
#define all(x) x.begin(),x.end()
#define lcm(a, b) ((a)*((b)/gcd(a,b)))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define buga cout<<1<<endl
#define bugb cout<<2<<endl
#define bugc cout<<3<<endl
#define bugd cout<<4<<endl
#define buge cout<<5<<endl
void fastIO (){ios_base::sync_with_stdio(false);cin.tie(0); cout.precision(20);}
ll A[MAX];
ll B[MAX];
ll C[MAX];
vector< ll >V;
vector< ll >Ans;
vector< pair<ll,ll> >VP;
map< ll,ll >M;
vector<ll>prime;
bool mark[MAX1];
ll N=1e7;
void seive (){int limit = sqrt(N*1.0)+2;mark[1]=1;for (int i=4;i<=N;i+=2)mark[i]=1;prime.push_back(2);for (int i=3;i<=N;i+=2){if(!mark[i]){prime.push_back(i);if(i<=limit){for (int j=i*i;j<=N;j+=i*2)mark[j]=1;}}}}
/*
ll bin_pow(ll o, ll s) {
if (s == 0) return 1;
if (s == 1) return o % mod;
ll d = bin_pow(o, s/2);
d = (d * 1ll * d) % mod;
if (s % 2 == 1)
d = (d * 1ll * o) % mod;
return d;
}
*/
ll dp[30][4][4],n;
ll rgb(int i, int source, int par){
if(source == par) return LINF;
if(i>n)return 0;
ll& ret = dp[i][par][source];
if(ret!=-1)return ret;
ll val = 1LL << 60;
val = min(val,rgb(i+1,0,source) + A[i]);
val = min(val,rgb(i+1,1,source) + B[i]);
val = min(val,rgb(i+1,2,source) + C[i]);
return ret = val;
}
int main ()
{
fastIO();
ll a,b,c,d,m,k,l,p,q,r,t,x,y;
ll cnt=0,cntr=0,sum=0,ans=1,check=0;
ll Max= 0,Max1=-1e18+1,Min=LINF;
string s,s1,s3,s4;
//cin>>n;
cin>>t;
while(t--){
cin>>n;
for(int i=1; i <= n; i++){
cin >> A[i] >> B[i] >> C[i];
}
mem(dp,-1);
ans = INT_MAX;
ans = min(ans,rgb(2,0,1) + A[1]);
ans = min(ans,rgb(2,1,2) + B[1]);
ans = min(ans,rgb(2,2,3) + C[1]);
printf("Case %lld: %lld\n",++cntr,ans);
}
return 0;
}