-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1034162_UVA348_20160717.c
82 lines (70 loc) · 1.46 KB
/
A1034162_UVA348_20160717.c
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
/*輸入一串矩陣的維度,輸出最佳相乘的順序,使得計算過程中用到的乘法最次數最少
ex:
input
3
1 5
5 20
20 1
ouput
Case 1: (A1 x (A2 x A3))
*/
#include<stdio.h>
int s[200][200];
int p[1000],n;
void print(int i,int j){
if(i==j)
{
printf("A%d",i);
}
else{
printf("(");
print(i,s[i][j]);
printf(" x ");
print(s[i][j]+1,j);
printf(")");
}
}
void matrix_multi()
{
int dp[200][200];
int i,j,k,l;
for(i=0;i<=n;i++)dp[i][i]=0;
for(l=2;l<=n;l++)
{
for(i=1;i<=n-l+1;i++)
{
j=i+l-1;
dp[i][j]=-1;
for(k=i;k<j;k++)
{
int q=dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
if(dp[i][j]==-1||dp[i][j]>q)
{
s[i][j]=k;
dp[i][j]=q;
}
}
}
}
print(1,n);
}
main(){
int i,x,ca;
for(ca=1;;ca++)
{
scanf("%d",&n);
if(n==0)break;
scanf("%d",&p[0]);
for(i=1;i<2*n;i++)
{
scanf("%d",&x);
if(i%2!=0)
{
p[(i+1)/2]=x;
}
}
printf("Case %d: ",ca);
matrix_multi();
puts("");
}
}