Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

{ PUNCTAJ OBTINUT: 3.

70/5 }

{$M 60000,0,655360}

program P01;

label fin;

type matr=array[1..100,1..100] of byte;

var f:text;

i1,j1,i,j,k,m,n,s,p,ncomp,puti,putj,l1i,l1j,l2i,l2j,nmetr,ntram:integer;

cda,cnu:array[1..100,1..100] of integer;

mcrit,tram:array[1..100,1..100] of byte;

cost:longint;

maxdf,nrdf:array[0..100] of integer;

vizit:array[1..100] of boolean;

compon,comp2:array[1..100] of byte;

metrou,link:^matr;

ok1,ok2,cdone:boolean;

procedure df(nod,tata:integer);

var s:integer;

max:integer;

begin

nrdf[nod]:=nrdf[tata]+1;

maxdf[nod]:=nrdf[nod];

vizit[nod]:=true;

if not cdone then compon[nod]:=ncomp;

for s:=1 to n do
if (tram[nod,s]=1) then begin

if (vizit[s]) and (nrdf[s]<maxdf[nod]) and (s<>tata)

then maxdf[nod]:=nrdf[s];

if (not vizit[s]) then begin

df(s,nod);

end;

end;

max:=101;

for s:=1 to n do begin

if (tram[nod,s]=1) and (nrdf[s]>nrdf[nod]) then begin

if (maxdf[s]<max) then max:=maxdf[s];

if (maxdf[s]>nrdf[nod]) then begin

mcrit[nod,s]:=1;

mcrit[s,nod]:=1;

end;

end;

end;

if max<maxdf[nod] then maxdf[nod]:=max;

end;

procedure cautacomp(nod:integer);

var s:integer;

begin

comp2[nod]:=1;

vizit[nod]:=true;

for s:=1 to n do

if (tram[nod,s]=1) and (not vizit[s]) then cautacomp(s);


end;

begin

assign(f,'tramvai.in');

reset(f);

readln(f,n,s);

for i:=1 to n do

for j:=1 to n do begin

cda[i,j]:=-1000;

cnu[i,j]:=-1000;

end;

for k:=1 to s do begin

readln(f,i,j,cnu[i,j],cda[i,j]);

cnu[j,i]:=cnu[i,j];

cda[j,i]:=cda[i,j];

end;

close(f);

cost:=0;

for i:=1 to n-1 do

for j:=i+1 to n do begin

mcrit[i,j]:=0;

if (cnu[i,j]<>-1000) then begin

cost:=cost+cnu[i,j];

tram[i,j]:=1;

tram[j,i]:=1;

end
else begin

tram[i,j]:=0;

tram[j,i]:=0;

end;

end;

nrdf[0]:=0;

ncomp:=0;

for i:=1 to n do begin

vizit[i]:=false;

compon[i]:=0;

end;

cdone:=false;

for i:=1 to n do

if compon[i]=0 then begin

inc(ncomp);

df(i,0);

end;

cdone:=true;

new(metrou);

new(link);

nmetr:=0;

for i:=1 to n do

for j:=1 to n do begin

metrou^[i,j]:=0;

if (i=j) then link^[i,j]:=1

else link^[i,j]:=0;

end;
for k:=1 to ncomp do begin

for i:=1 to n do

if (compon[i]=k) then

for j:=1 to n do

if compon[j]=k then

if (cda[i,j]<cnu[i,j]) and (tram[i,j]=1) then begin

if mcrit[i,j]=0 then begin

tram[i,j]:=0;

tram[j,i]:=0;

cost:=cost+(cda[i,j]-cnu[i,j]);

for i1:=1 to n do vizit[i1]:=false;

df(i,0);

end

else begin

ok1:=false;

ok2:=false;

for i1:=1 to n do begin

vizit[i1]:=false;

comp2[i1]:=0;

end;

tram[i,j]:=0;

tram[j,i]:=0;

cautacomp(i);

for j1:=1 to n do
if (comp2[j1]=1) and (compon[j1]=compon[i]) then

for i1:=1 to n do

if (compon[i1]<>compon[j1]) and (cda[i1,j1]=-1000) and (j1<>i1) then begin

l1i:=i1;

l2i:=j1;

ok1:=true;

break;

end;

if not ok1 then begin

for j1:=1 to n do

if (comp2[j1]=1) and (compon[j1]=compon[i]) then

for i1:=1 to n do

if (compon[i1]=compon[i]) and (comp2[i1]=0)

and (cda[i1,j1]=-1000) and (j1<>i1) then begin

l1i:=i1;

l2i:=j1;

ok1:=true;

break;

end;

end;

if (ok1) and (compon[l1i]<>compon[i]) then begin

for j1:=1 to n do

if (comp2[j1]=0) and (compon[j1]=compon[i]) then

for i1:=1 to n do

if (compon[i1]<>compon[j1]) and (cda[i1,j1]=-1000) and (j1<>i1) then begin

l1j:=i1;

l2j:=j1;
ok2:=true;

break;

end;

if (not ok2) then begin

for j1:=1 to n do

if (comp2[j1]=0) and (compon[j1]=compon[i]) then

for i1:=1 to n do

if (compon[i1]=compon[j1]) and (cda[i1,j1]=-1000) and (j1<>i1) then begin

l1j:=i1;

l2j:=j1;

ok2:=true;

break;

end;

end;

end;

if ok1 or ok2 then begin

cost:=cost+(cda[i,j]-cnu[i,j]);

if ok1 then begin

metrou^[l2i,l1i]:=1;metrou^[l1i,l2i]:=1;

end;

if ok2 then begin

metrou^[l2j,l1j]:=1;metrou^[l1j,l2j]:=1;

end;

df(i,0);

end

else begin

tram[i,j]:=1;
tram[j,i]:=1;

end;

end;

end;

end;

repeat

ok1:=true;

for i:=1 to n do

for j:=1 to n do

if (compon[i]<>compon[j]) and (metrou^[i,j]=1) then begin

l1i:=compon[j];

for k:=1 to n do

if (compon[k]=l1i) then begin

compon[k]:=compon[i];

ok1:=false;

end;

end;

until ok1;

for k:=2 to ncomp do begin

for i:=1 to n do

if (compon[i]<>k) then

for j:=1 to n do

if (compon[j]=k) then begin

metrou^[i,j]:=1;
metrou^[j,i]:=1;

goto fin;

end;

fin:

end;

assign(f,'tramvai.out');

rewrite(f);

writeln(f,cost);

ntram:=0;

nmetr:=0;

for i:=1 to n-1 do

for j:=i+1 to n do begin

if tram[i,j]=1 then inc(ntram);

if metrou^[i,j]=1 then inc(nmetr);

end;

writeln(f,ntram);

for i:=1 to n-1 do

for j:=i+1 to n do

if tram[i,j]=1 then writeln(f,i,' ',j);

writeln(f,nmetr);

for i:=1 to n-1 do

for j:=i+1 to n do

if metrou^[i,j]=1 then writeln(f,i,' ',j);

close(f);

end.

You might also like