10461 - Difference

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

10461 - Difference

Post by cyfra »

Hi!


I wonder what's wrong... I make it in this way:

sum1= sum of all verticles, with must be finished before this task.
sum2= sum of all verticles, except these one which needs our task to be finished...

And the answer is sum2-sum1.

Can anyone give me any tricky input for this task. Or tell me if my algorithm is right ...

Thanks in advence :wink:
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

I use the same algorithm to get accepted~
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

Post by cyfra »

So it is propably my mistake in my program... But I can't find it...

Here is my code :


[pascal]
TYPE
pelem = ^elem;
elem = record
dokad:longint;
next:pelem;
end;

{ a dynamic queue }

VAR
counter,y,sum1,sum2,pole,qw,q,x,a,b,n,m:longint;
tab1,tab2:array[1..5000] of pelem;
p:pelem;
koszt:array[1..5000] of longint;
used:array[1..5000] of boolean;


Procedure DFS(co:longint);
{ this procedure checks which jobs can't be done without doing our job }
VAR
p:pelem;

begin
used[co]:=true;
p:=tab1[co];
while p<>nil do
begin
if used[p^.dokad]=false then DFS(p^.dokad);
p:=p^.next;
end;
end;


Procedure DFS2(co:longint);
{ this one checks which jobs must be done before our job }
VAR
p:pelem;

begin
used[co]:=true;
p:=tab2[co];
while p<>nil do
begin
if used[p^.dokad]=false then DFS2(p^.dokad);
p:=p^.next;
end;
end;


Procedure dodaj1(zm1,zm2:longint);
{ adds a connection beetween zm1 and zm2 }
begin
New(p);
p^.dokad:=zm2;
p^.next:=tab1[zm1];
tab1[zm1]:=p;
end;

Procedure dodaj2(zm1,zm2:longint);
{ the same as above but to the other queue}
begin
New(p);
p^.dokad:=zm2;
p^.next:=tab2[zm1];
tab2[zm1]:=p;
end;


begin
counter:=0;

repeat

counter:=counter+1;

readln(n,m);

if (n<>0) or (m<>0) then
begin


for x:=1 to 500 do tab1[x]:=nil;
for x:=1 to 500 do tab2[x]:=nil;
for x:=1 to n do read(koszt[x]);
readln;

for x:=1 to m do
begin
readln(a,b);
dodaj1(a,b);
dodaj2(b,a);
end;

Writeln('Case #',counter,':');
readln(q);

for qw:=1 to q do
begin
read(pole);

for y:=1 to n do used[y]:=false;
sum1:=0; sum2:=0;
DFS2(pole);
for x:=1 to n do if used[x] then sum1:=sum1+koszt[x];

for y:=1 to n do used[y]:=false;
DFS(pole);
for x:=1 to n do if not used[x] then sum2:=sum2+koszt[x];
sum2:=sum2+koszt[pole];

Writeln(sum2-sum1);


end;
Writeln; readln; readln;


end;

until (n=0) and (m=0);

end.

[/pascal]


Do You have any idea what's wrong ??

Thanks in advance :D
Post Reply

Return to “Volume 104 (10400-10499)”