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 1076. Trash

Why I got WA?
Posted by zealot 3 Apr 2003 08:12
My solution here:

const max=160;
var g:array[1..max,1..max] of integer;
    x,y:array[1..max] of boolean;
    link,lx,ly:array[1..max] of longint;
    n,num:longint;
procedure readdata;
var i,j:integer;
begin
readln(n);
for i:=1 to n do
 begin
  for j:=1 to n-1 do read(g[i,j]);
  readln(g[i,n]);
 end;
end;
function find(i:integer):boolean;
var k,p:integer;
begin
find:=true;
x[i]:=true;
for k:=1 to n do
 if not y[k] and (lx[i]+ly[k]=g[i,k]) then
  begin
   p:=link[k];link[k]:=i;y[k]:=true;
   if (p=0) or find(p) then exit;
   link[k]:=p;
  end;
find:=false;
end;
procedure main;
var i,j,d:integer;
begin
for i:=1 to n do
 for j:=1 to n do
  if g[i,j]>lx[i] then lx[i]:=g[i,j];
for i:=1 to n do
 repeat fillchar(x,sizeof(x),0);fillchar(y,sizeof(y),0);
        if find(i) then break;
        for i:=1 to n do if x[i] then
         for j:=1 to n do if not y[j] then
          if lx[i]+ly[j]-g[i,j]<d then d:=lx[i]+ly[j]-g[i,j];
        for i:=1 to n do if x[i] then lx[i]:=lx[i]-d;
        for j:=1 to n do if y[j] then ly[j]:=ly[j]+d;
        until false;
end;
function did(a,b:integer):boolean;
var k:integer;
    m:longint;
begin
k:=a;did:=false;m:=a;
while k>1 do
 begin
  dec(k);m:=m*k;
  if m>b then exit;
 end;
if m=b then did:=true;
end;
procedure print;
var i,j:integer;
begin
for i:=1 to n do
 for j:=1 to n do
  if link[j]=i then
   else
    begin
     if (i>=link[j])or(i>=6) then num:=num+g[i,j]
      else if did(i,link[j]) then
       else num:=num+g[i,j];
    end;
writeln(num);
end;
BEGIN
readdata;
main;
print;
END.