ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1029. Ministry

help why i got WA
Posted by ruwelcome 28 Jan 2003 13:30
var
i,j,k,m,n,min,minn,x,y,x1,y1:longint;
ans:array[0..50000] of integer;
money:Array[0..500,0..100] of longint;
a:Array[0..500,0..100] of longint;
cx,cy:Array[0..500,0..100] of integer;
begin
  fillchar(money,sizeof(money),255);
  fillchar(cx,sizeof(cx),0);
  fillchar(cy,sizeof(cy),0);
  fillchar(a,sizeof(a),0);
  readln(m,n);
  for i:=1 to m do
  begin
    for j:=1 to n do
    read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  money[1,i]:=a[1,i];
  for i:=2 to m do
  begin
    for j:=1 to n do
    begin
      money[i,j]:=money[i-1,j]+a[i,j];
      cx[i,j]:=i-1;
      cy[i,j]:=j;
    end;
    for j:=2 to n do
    if ((money[i,j]>money[i,j-1]+a[i,j]) or (money[i,j]=-1)) and
(money[i,j-1]<>-1) then
    begin
      money[i,j]:=money[i,j-1]+a[i,j];
      cx[i,j]:=i;
      cy[i,j]:=j-1;
    end;
    for j:=n-1 downto 1 do
    if ((money[i,j]>money[i,j+1]+a[i,j]) or (money[i,j]=-1)) and
(money[i,j+1]<>-1) then
    begin
      money[i,j]:=money[i,j+1]+a[i,j];
      cx[i,j]:=i;
      cy[i,j]:=j+1;
    end;
  end;
  min:=money[m,1];
  minn:=1;
  for i:=2 to n do
  if min>money[m,i] then
  begin
    min:=money[m,i];
    minn:=i;
  end;
  y:=minn;
  x:=m;
  k:=1;
  ans[k]:=y;
  while cx[x,y]<>0 do
  begin
    k:=k+1;
    x1:=x;
    y1:=y;
    x:=cx[x1,y1];
    y:=cy[x1,y1];
    ans[k]:=y;
  end;
  for i:=k downto 1 do
  write(ans[i],' ');
  writeln;
end.