-
Notifications
You must be signed in to change notification settings - Fork 13
/
Trigraph.cpp
99 lines (65 loc) · 1.89 KB
/
Trigraph.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
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
long long int min(long long int x,long long int y,long long int z,long long int w)
{
long long int k=(x<y)?x:y;
k=(k<z)?k:z;
k=(k<w)?k:w;
return k;
}
long long int min(long long int x,long long int y,long long int z)
{
long long int k=(x<y)?x:y;
k=(k<z)?k:z;
return k;
}
long long int min(long long int x,long long int y)
{
long long int k=(x<y)?x:y;
return k;
}
int main()
{
int n,count=0;
while(true)
{
cin>>n;
count++;
if(n==0)
break;
long long int cost[n][3];
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
{
scanf("%lld",&cost[i][j]);
}
}
cost[0][0]=2147483647;
cost[0][2]=cost[0][1]+cost[0][2];
/*cost[0][0]=INF;
cost[0][2]=INF;*/
long long int nodescost[n][3];
for(int i=0;i<3;i++)
nodescost[0][i]=cost[0][i];
for(int i=1;i<n;i++)
for(int j=0;j<3;j++)
{
if(j == 1)
nodescost[i][j]=min(nodescost[i-1][j-1],nodescost[i-1][j],nodescost[i-1][j+1],nodescost[i][j-1])+cost[i][j];
else if(j == 2)
nodescost[i][j]=min(nodescost[i-1][j-1],nodescost[i-1][j],nodescost[i][j-1])+cost[i][j];
else if(j == 0)
nodescost[i][j]=min(nodescost[i-1][j],nodescost[i-1][j+1])+cost[i][j];
}
//cout<<count<<". "<<nodescost[n-1][1]<<endl;
//cout<<nodescost[n-1][0]<<endl;
printf("%d.",count);
printf(" %lld\n",nodescost[n-1][1]);
//cout<<nodescost[n-1][1]<<" "<<nodescost[n-1][2]<<endl;
}
system ("pause");
return 0;
}