程序碎片- 矩阵乘法优化(dp,递归)
2009-12-21 16:07
531 查看
using
System
;
using
System
.
Collections
.
Generic
;
namespace
SDProject
{
public
class
MartixMultiply
{
private
List
<
Martix
>
MList
;
public
MartixMultiply
()
{
initial
();
}
private
void
initial
()
{
Martix m1
=
new
MartixMultiply
.
Martix
(
2
,
5
);
Martix m2
=
new
MartixMultiply
.
Martix
(
5
,
100
);
Martix m3
=
new
MartixMultiply
.
Martix
(
100
,
4
);
Martix m4
=
new
MartixMultiply
.
Martix
(
4
,
50
);
Martix m5
=
new
MartixMultiply
.
Martix
(
50
,
6
);
Martix
[]
array
={
m1
,
m2
,
m3
,
m4
,
m5
};
this
.
MList
=
new
List
<
Martix
>(
array
);
}
int
[,]
M
=
new
int
[
5
,
5
];
public
void
GetBestSolutionByDP
()
{
//
设局部最优解是
M
【
i
,
j
】设矩阵
Ai
的维数是
P(i-1),
和
P(i)
//
则
M
【
i
,
j
】
=min{M
【
i
,
k
】
+M
【
k+1,j
】
+P(i-1)*P(k)*P(j)}
其中
i<=k<j
for
(
int
i
=
0
;
i
<
5
;
i
++)
for
(
int
j
=
0
;
j
<
5
;
j
++)
{
if
(
j
<=
i
)
{
M
[
i
,
j
]=
0
;
}
else
{
M
[
i
,
j
]=
Int32
.
MaxValue
;
}
}
int
finalResult
=
getOpt
(
0
,
4
);
Console
.
WriteLine
(
finalResult
);
}
private
int
getOpt
(
int
from
,
int
to
)
{
int
costform2K
;
int
costK2to
;
int
costMultiply2
;
int
costTotal
=
Int32
.
MaxValue
;
if
(
M
[
from
,
to
]!=
Int32
.
MaxValue
)
{
return
M
[
from
,
to
];
}
else
{
for
(
int
k
=
from
;
k
<
to
;
k
++)
{
costform2K
=
getOpt
(
from
,
k
);
costK2to
=
getOpt
(
k
+
1
,
to
);
costMultiply2
=
MList
[
from
].
orderX
*
MList
[
k
].
orderY
*
MList
[
to
].
orderY
;
int
tempcost
=
costform2K
+
costK2to
+
costMultiply2
;
if
(
costTotal
>
tempcost
)
{
costTotal
=
tempcost
;
M
[
from
,
to
]=
tempcost
;
}
}
return
costTotal
;
}
}
class
Martix
{
public
Martix
(
int
x
,
int
y
)
{
orderX
=
x
;
orderY
=
y
;
}
public
int
orderX
{
set
;
get
;}
public
int
orderY
{
set
;
get
;}
}
}
}
System
;
using
System
.
Collections
.
Generic
;
namespace
SDProject
{
public
class
MartixMultiply
{
private
List
<
Martix
>
MList
;
public
MartixMultiply
()
{
initial
();
}
private
void
initial
()
{
Martix m1
=
new
MartixMultiply
.
Martix
(
2
,
5
);
Martix m2
=
new
MartixMultiply
.
Martix
(
5
,
100
);
Martix m3
=
new
MartixMultiply
.
Martix
(
100
,
4
);
Martix m4
=
new
MartixMultiply
.
Martix
(
4
,
50
);
Martix m5
=
new
MartixMultiply
.
Martix
(
50
,
6
);
Martix
[]
array
={
m1
,
m2
,
m3
,
m4
,
m5
};
this
.
MList
=
new
List
<
Martix
>(
array
);
}
int
[,]
M
=
new
int
[
5
,
5
];
public
void
GetBestSolutionByDP
()
{
//
设局部最优解是
M
【
i
,
j
】设矩阵
Ai
的维数是
P(i-1),
和
P(i)
//
则
M
【
i
,
j
】
=min{M
【
i
,
k
】
+M
【
k+1,j
】
+P(i-1)*P(k)*P(j)}
其中
i<=k<j
for
(
int
i
=
0
;
i
<
5
;
i
++)
for
(
int
j
=
0
;
j
<
5
;
j
++)
{
if
(
j
<=
i
)
{
M
[
i
,
j
]=
0
;
}
else
{
M
[
i
,
j
]=
Int32
.
MaxValue
;
}
}
int
finalResult
=
getOpt
(
0
,
4
);
Console
.
WriteLine
(
finalResult
);
}
private
int
getOpt
(
int
from
,
int
to
)
{
int
costform2K
;
int
costK2to
;
int
costMultiply2
;
int
costTotal
=
Int32
.
MaxValue
;
if
(
M
[
from
,
to
]!=
Int32
.
MaxValue
)
{
return
M
[
from
,
to
];
}
else
{
for
(
int
k
=
from
;
k
<
to
;
k
++)
{
costform2K
=
getOpt
(
from
,
k
);
costK2to
=
getOpt
(
k
+
1
,
to
);
costMultiply2
=
MList
[
from
].
orderX
*
MList
[
k
].
orderY
*
MList
[
to
].
orderY
;
int
tempcost
=
costform2K
+
costK2to
+
costMultiply2
;
if
(
costTotal
>
tempcost
)
{
costTotal
=
tempcost
;
M
[
from
,
to
]=
tempcost
;
}
}
return
costTotal
;
}
}
class
Martix
{
public
Martix
(
int
x
,
int
y
)
{
orderX
=
x
;
orderY
=
y
;
}
public
int
orderX
{
set
;
get
;}
public
int
orderY
{
set
;
get
;}
}
}
}
相关文章推荐
- 程序碎片- 矩阵乘法优化(dp,循环)
- 【bzoj2476】战场的数目 矩阵乘法优化dp
- bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼 (矩阵乘法优化DP)
- [矩阵乘法优化DP] Topcoder SRM554. TheBrickTowerHardDivOne
- 矩阵乘法优化DP
- 矩阵乘法优化DP
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
- 程序算法艺术与实践:递归策略之矩阵乘法问题
- BZOJ1009 GT考试 (DP 矩阵乘法优化)
- Codevs 1305 Freda的道路(矩阵乘法 DP优化)
- BZOJ 1875 SDOI 2009 HH去散步 矩阵乘法优化DP
- 【矩阵乘法优化DP】BZOJ1875 [SDOI2009]HH去散步
- 51nod 1583 犯罪计划——矩阵乘法优化dp
- Poj 3734 Blocks(DP,矩阵乘法优化)
- 关于矩阵乘法优化dp(入门+斐波那契模板题)
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- 1009: [HNOI2008]GT考试 矩阵乘法优化DP+KMP
- 一个矩阵乘法优化期望dp的题
- BZOJ 1875 [SDOI 2009] HH去散步 (DP,矩阵乘法优化)
- BZOJ 3120 Line【矩阵乘法优化dp