最小代价问题
来源:互联网 发布:精美立体数据图表 ppt 编辑:程序博客网 时间:2024/06/06 20:50
最小代价问题
Time Limit:1000MS Memory Limit:65536K
Total Submit:207 Accepted:72
Description
设有一个n×m(小于100)的方格(如图所示),在方格中去掉某些点,方格中的数字代表距离(为小于100的数,如果为0表示去掉的点),试找出一条从A(左上角)到B(右下角)的路径,经过的距离和为最小(此时称为最小代价),从A出发的方向只能向右,或者向下。
Input
Output
Sample Input
4 4
4 10 7 0
3 2 2 9
0 7 0 4
11 6 12 1
Sample Output
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)
24
var
i,j,n,m:longint;a:array[1..1000,1..1000]of longint;
f:array[1..1000,1..1000,1..2]of longint;
function mix(a,b:longint):longint;
begin
if a<b then exit(a);
exit(b);
end;
procedure dg(x,y:longint);
begin
if (x<1) or (y<1) then exit;
if f[x,y,2]=1 then dg(x,y-1)
else dg(x-1,y);
if (x<>n) or (y<>m) then write('(',x,',',y,')->')
else write('(',x,',',y,')');
end;
begin
read(n,m);
for i:=1 to n do
for j:=1 to m do
read(a[i,j]);
f[1,1,1]:=a[1,1];
for i:=2 to n do
if (a[1,i]<>0)and(f[1,i-1,1]<>0) then
begin
f[1,i,1]:=f[1,i-1,1]+a[1,i];
f[1,i,2]:=1;
end;
for j:=2 to m do
if (a[j,1]<>0)and(f[j-1,1,1]<>0) then
begin
f[j,1,1]:=f[j-1,1,1]+a[j,1];
f[j,1,2]:=2;
end;
for i:=2 to n do
for j:=2 to m do
if (a[i,j]<>0) then
begin
if (f[i-1,j,1]=0)and(f[i,j-1,1]<>0)then
begin
f[i,j,1]:=f[i,j-1,1]+a[i,j];
f[i,j,2]:=1;
end;
if (f[i,j-1,1]=0)and(f[i-1,j,1]<>0) then
begin
f[i,j,1]:=f[i-1,j,1]+a[i,j];
f[i,j,2]:=2;
end;
if (f[i-1,j,1]<>0)and(f[i,j-1,1]<>0) then
begin
f[i,j,1]:=mix(f[i-1,j,1],f[i,j-1,1])+a[i,j];
if mix(f[i-1,j,1],f[i,j-1,1])=f[i-1,j,1] then f[i,j,2]:=2
else f[i,j,2]:=1;
end;
end;
dg(n,m);
writeln;
write(f[n,m,1]-a[n,m]);
end.
1 0
- 最小代价问题
- 最小代价问题
- 最小代价问题
- [DP]最小代价问题
- 最小代价问题
- 最小代价问题
- 买一送一的最小代价问题
- SSL 1460——最小代价问题
- 最小代价
- 最小代价;;;;
- 最小代价
- 最小代价
- POJ1251(基础的最小代价生成树问题)
- 动态规划系列问题-最小编辑代价
- 霍夫曼树之切割木板最小总代价问题
- 最小代价子母树
- 最小代价树
- 最小代价树
- opencv dft
- tmux进阶之tmuxinator
- Plupload上传插件使用
- Bootstrap multiSelect 在页面POSTBack后不显示
- C语言指针导学(3)——指针与数组的“爱恨情仇”
- 最小代价问题
- (ssl1045)P1048 采药(0/1背包)
- HTML5 footer元素
- spring事务
- PAT-B 1047. 编程团体赛(20)
- Swift 闭包传值 反向传值
- Maven实践(七)---仓库
- oracle的job定时执行存储过程例子和job基本知识介绍
- java系列理论----对象与类