// // 4th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // Matrix problem using recursive algorithm. It's simple but poor in its performance cause their exists lots of repeated calculation. // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/26. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
int p[1010]; int s[1010][1010];
intre_matrix(int head,int tail) { if (head >= tail) return0; int ret = INF; int tep; for(int i=head;i<tail;i++) { tep = re_matrix(head, i) + re_matrix(i+1, tail) + p[head-1]*p[i]*p[tail]; if (tep<ret) { s[head][tail] = i; ret = tep; } } return ret; } voidOutput_Ans(int head, int tail) { if (head >= tail) return; Output_Ans(head, s[head][tail]); Output_Ans(s[head][tail]+1, tail); cout << "A" << head << "-" << s[head][tail]; cout << " * A" << s[head][tail]+1 << "-" << tail << endl; }
// // 4th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // Matrix problem using dynamic programming. Manually cutting brunches makes it much more efficent than the formal one. // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/26. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
int p[1010]; int s[1010][1010]; int m[1010][1010];
intdp_matrix(int n) { for (int i=0;i<=n;i++) m[i][i]=0; for (int len = 2;len<=n;len++) for (int head = 1;head<=n-len+1;head++) { int tail = head+len-1; m[head][tail] = INF; int tep; for (int i=head;i<tail;i++) { tep = m[head][i] + m[i+1][tail] + p[head-1]*p[i]*p[tail]; if (tep < m[head][tail]) { m[head][tail] = tep; s[head][tail] = i; } } } return m[1][n]; } voidOutput_Ans(int head, int tail) { if (head >= tail) return; Output_Ans(head, s[head][tail]); Output_Ans(s[head][tail]+1, tail); cout << "A" << head << "-" << s[head][tail]; cout << " * A" << s[head][tail]+1 << "-" << tail << endl; }
// // 4th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // Matrix problem using memorized search. In fact, memorized search can replace dp in almost all problems. // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/26. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
int p[1010]; int s[1010][1010]; int m[1010][1010];
intdfs_matrix(int head,int tail) { if (head >= tail) return0; if (m[head][tail]!=0) return m[head][tail]; m[head][tail] = INF; int tep; for(int i=head;i<tail;i++) { tep = dfs_matrix(head, i) + dfs_matrix(i+1, tail) + p[head-1]*p[i]*p[tail]; if (tep<m[head][tail]) { s[head][tail] = i; m[head][tail] = tep; } } return m[head][tail]; } voidOutput_Ans(int head, int tail) { if (head >= tail) return; Output_Ans(head, s[head][tail]); Output_Ans(s[head][tail]+1, tail); cout << "A" << head << "-" << s[head][tail]; cout << " * A" << s[head][tail]+1 << "-" << tail << endl; }