程序碎片- 矩阵乘法优化(dp,循环)
来源:互联网 发布:mac office 中文版 编辑:程序博客网 时间:2024/04/29 23:07
下班了换个eclipse写写,感觉还蛮好的。
这个循环的原理是,沿着矩阵的对角线,一层层向上计算,
发现这样的过程中,前面的条件都已经算好了。所以就不用递归了。
import java.util.ArrayList;
public class MatrixMultiply {
public MatrixMultiply() {
Matrix m1 = new Matrix(2, 5);
Matrix m2 = new Matrix(5, 100);
Matrix m3 = new Matrix(100, 4);
Matrix m4 = new Matrix(4, 50);
Matrix m5 = new Matrix(50, 6);
MList.add(m1);
MList.add(m2);
MList.add(m3);
MList.add(m4);
MList.add(m5);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (i >= j)
M[i][j] = 0;
else
M[i][j] = Integer.MAX_VALUE;
}
}
private int[][] M = new int[5][5];
private ArrayList<Matrix> MList = new ArrayList<Matrix>();
public void getResult() {
// already fill the left-below half of the matrix, now fill the left-up
// part, the specialIndex is build by analysis matrix's structure
for (int specialIndex = 1; specialIndex < 5; specialIndex++) {
int xIndex = 0;
int yIndex = xIndex + specialIndex;
while (yIndex < 5) {
M[xIndex][yIndex] = getMax(xIndex, yIndex);
yIndex++;
xIndex++;
}
}
System.out.println("the optimized result is :" + M[0][4]);
}
private int getMax(int xIndex, int yIndex) {
if (M[xIndex][yIndex] != Integer.MAX_VALUE)
return M[xIndex][yIndex];
else {
int finalMin = Integer.MAX_VALUE;
int tempMin;
for (int k = xIndex; k < yIndex; k++) {
tempMin = M[xIndex][k] + M[k + 1][yIndex]
+ MList.get(xIndex).orderX * MList.get(k).orderY
* MList.get(yIndex).orderY;
if (finalMin > tempMin)
finalMin = tempMin;
}
return finalMin;
}
}
class Matrix {
public Matrix(int x, int y) {
orderX = x;
orderY = y;
}
int orderX;
int orderY;
}
}
- 程序碎片- 矩阵乘法优化(dp,循环)
- 程序碎片- 矩阵乘法优化(dp,递归)
- 矩阵乘法优化DP
- 矩阵乘法优化DP
- 【Contra】 矩阵乘法优化 dp
- BZOJ 3120 Line【矩阵乘法优化dp
- Codeforces 852B 矩阵乘法优化DP
- poj 3734 Blocks 矩阵乘法优化dp
- 【bzoj2510】弱题 概率dp+循环矩阵矩阵乘法
- bzoj 2510: 弱题 期望dp+循环矩阵乘法
- Python numpy 矩阵特殊加、乘法与循环优化
- sgu197:Nice Patterns Strike Back(状压dp+矩阵乘法优化)
- Poj 3734 Blocks(DP,矩阵乘法优化)
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- 1009: [HNOI2008]GT考试 矩阵乘法优化DP+KMP
- BZOJ1009 GT考试 (DP 矩阵乘法优化)
- 【矩阵乘法优化DP】BZOJ1875 [SDOI2009]HH去散步
- 【矩阵乘法优化DP】Codeforces 717D Dexterina’s Lab
- Asp.net Global.asax
- WIN网络编程-IOCP服务程序
- matlab基本运算与函数
- Arithmetic and Logical Operation
- C#中的多线程——入门
- 程序碎片- 矩阵乘法优化(dp,循环)
- matlab基础
- 网络访问速度变慢,原来是DNS设置的缘故
- matlab循环结构
- 广州技术沙龙改名为珠三角技术沙龙
- 开播了~!
- VB 单实例运行
- matlab选择结构
- 自定义jstl标签库