[Poj]3613——快速幂优化矩阵操作
2012-04-27 18:46
393 查看
[题目大意]给定一张带权无向图,求从S到E经过恰好N条边(可重复)的最短路
[分析题解] 我们用f[k,i]表示走过k条边,现在在i的最短路.显然,f[k,i]:=min(f[k-1,j]+w(j,i)) if j可以到达i
又题目给出的N的范围相对于图来说特别的大,又是这种非常单调的转移,这样的话,考虑用矩阵+快速幂来优化。
与普通的矩阵乘法比,将对应相乘求和操作改为对应相加取最小值操作即可
[个人代码]View Code 1 //10128532 perseawe 3613 Accepted 1016K 329MS Pascal 1242B 2012-04-27 18:35:45
2
3 Const
4 Inf=1005000000;
5
6 Type
7 TypeArr=Array [0..100+10,0..100+10] of Longint;
8
9 Var
N,T,S,E,tot:Longint;
hash:Array [0..1000+10] of Longint;
f,Ans:TypeArr;
Procedure Init;
var
i,L,u,v:Longint;
begin
readln(N,T,S,E);
tot:=0;
Filldword(f,sizeof(f) shr 2,Inf);
for i:=1 to T do
begin
readln(L,u,v);
if hash[u]=0 then begin inc(tot);hash[u]:=tot;end;
if hash[v]=0 then begin inc(tot);hash[v]:=tot;end;
f[hash[u],hash[v]]:=L;f[hash[v],hash[u]]:=L;
end;
S:=hash[S];E:=hash[E];
end;
Procedure Add(var p,q:TypeArr);
var
res:TypeArr;
i,j,k:Longint;
begin
Filldword(res,sizeof(res) shr 2,Inf);
for k:=1 to tot do
for i:=1 to tot do
for j:=1 to tot do
if p[i,k]+q[k,j]<res[i,j] then
res[i,j]:=p[i,k]+q[k,j];
p:=res;
end;
Procedure Main;
var
I:Longint;
begin
Filldword(Ans,sizeof(Ans) shr 2,Inf);
For I:=1 to tot do ans[I,I]:=0;
Repeat
if N and 1=1 then Add(Ans,f);
Add(f,f);
N:=N shr 1;
Until N=0;
end;
Procedure Print;
begin
writeln(Ans[S,E]);
end;
Begin
Init;
Main;
Print;
End.[相关链接]
[启发总结] 挺厉害的矩阵
[分析题解] 我们用f[k,i]表示走过k条边,现在在i的最短路.显然,f[k,i]:=min(f[k-1,j]+w(j,i)) if j可以到达i
又题目给出的N的范围相对于图来说特别的大,又是这种非常单调的转移,这样的话,考虑用矩阵+快速幂来优化。
与普通的矩阵乘法比,将对应相乘求和操作改为对应相加取最小值操作即可
[个人代码]View Code 1 //10128532 perseawe 3613 Accepted 1016K 329MS Pascal 1242B 2012-04-27 18:35:45
2
3 Const
4 Inf=1005000000;
5
6 Type
7 TypeArr=Array [0..100+10,0..100+10] of Longint;
8
9 Var
N,T,S,E,tot:Longint;
hash:Array [0..1000+10] of Longint;
f,Ans:TypeArr;
Procedure Init;
var
i,L,u,v:Longint;
begin
readln(N,T,S,E);
tot:=0;
Filldword(f,sizeof(f) shr 2,Inf);
for i:=1 to T do
begin
readln(L,u,v);
if hash[u]=0 then begin inc(tot);hash[u]:=tot;end;
if hash[v]=0 then begin inc(tot);hash[v]:=tot;end;
f[hash[u],hash[v]]:=L;f[hash[v],hash[u]]:=L;
end;
S:=hash[S];E:=hash[E];
end;
Procedure Add(var p,q:TypeArr);
var
res:TypeArr;
i,j,k:Longint;
begin
Filldword(res,sizeof(res) shr 2,Inf);
for k:=1 to tot do
for i:=1 to tot do
for j:=1 to tot do
if p[i,k]+q[k,j]<res[i,j] then
res[i,j]:=p[i,k]+q[k,j];
p:=res;
end;
Procedure Main;
var
I:Longint;
begin
Filldword(Ans,sizeof(Ans) shr 2,Inf);
For I:=1 to tot do ans[I,I]:=0;
Repeat
if N and 1=1 then Add(Ans,f);
Add(f,f);
N:=N shr 1;
Until N=0;
end;
Procedure Print;
begin
writeln(Ans[S,E]);
end;
Begin
Init;
Main;
Print;
End.[相关链接]
[启发总结] 挺厉害的矩阵
相关文章推荐
- poj 3734 Blocks (矩阵快速幂优化的动态规划)
- poj 3744 概率dp 矩阵快速幂优化
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
- poj 3744 Scout YYF I(矩阵快速幂优化dp)
- POJ 3420 Quad Tiling(状压DP 用矩阵快速幂优化)
- poj 3735 大数量重复操作问题(矩阵快速幂)
- POJ 3613 Cow Relays k步最短路 (floyd + 矩阵快速幂)
- poj 3613 矩阵快速幂变形(恰好k条边最短路)
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
- POJ 3613 Cow Relays (floyd + 矩阵快速幂)
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
- poj 3613 Cow Relays【矩阵快速幂+Floyd】
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
- poj 3735Training little cats(矩阵快速幂 完成模拟 注意优化)
- POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)
- poj 3613 floyd+二分矩阵相乘(快速幂)
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
- POJ 3745 : Training little cats(矩阵快速幂加优化)
- POJ 3735 Training little cats 解题报告(矩阵构造+快速幂优化)
- POJ 3040 浅谈矩阵快速幂优化LogN斐波拉契函数求解