Programozás‎ > ‎Feladatok‎ > ‎Vasúti menetrend‎ > ‎Megoldás‎ > ‎

lb_vasut.pas

Letöltés: lb_vasut.pas

{$R+}
PROGRAM vasut;
USES crt;
VAR
    n: INTEGER;
    t,na,nb,av,bv: ARRAY [1..100] OF INTEGER;   //fordulasi ido, a/b-bol indulo vonatok szama, a szukseges vonatok szama a megoldashoz
    ko,kp,vo,vp: ARRAY [1..100,1..200] OF INTEGER;  //indulo ora/perc, erkezesi ora/perc
    rio,rip,reo,rep: ARRAY [1..100,1..200] OF INTEGER;  //rendezett indulo/erkezo ora/perc
    re,rab: ARRAY[1..100,1..200] OF BOOLEAN;    //rendeztuk-e mar az adott adatsort, rendezesnel a-bol b-be megy-e
    
FUNCTION C2I(c: CHAR): INTEGER; //CHARTOINT
    BEGIN
        C2I:=ORD(c)-48;
    END;
 
PROCEDURE beolvas(honnan: STRING);
    VAR
        f: TEXT;
        i,j: INTEGER;
        tm1,tm2: CHAR;
    BEGIN
        ASSIGN(f,honnan);
        RESET(f);
        READLN(f,n);
        FOR i:=1 TO n DO
            BEGIN
                READ(f,t[i]);
                READ(f,na[i]);
                READLN(f,nb[i]);
                FOR j:=1 TO (na[i]+nb[i]) DO
                    BEGIN
                        READ(f,tm1);
                        READ(f,tm2);
                        ko[i,j]:=C2I(tm1)*10+C2I(tm2);
                        READ(f,tm1);
                        READ(f,tm1);
                        READ(f,tm2);
                        kp[i,j]:=C2I(tm1)*10+C2I(tm2);
                        READ(f,tm1);
                        //-
                        READ(f,tm1);
                        READ(f,tm2);
                        vo[i,j]:=C2I(tm1)*10+C2I(tm2);
                        READ(f,tm1);
                        READ(f,tm1);
                        READ(f,tm2);
                        vp[i,j]:=C2I(tm1)*10+C2I(tm2)+t[i];
                        IF vp[i,j]>59 THEN
                            BEGIN
                                vp[i,j]:=vp[i,j]-60;
                                vo[i,j]:=vo[i,j]+1;
                            END;
                        READLN(f);
                    END;
            END;
        CLOSE(f);
    END;
 
PROCEDURE rendez;
    VAR
        i,j,k,mo,mp,ms,r: INTEGER;  //ciklusvalt, minimum ora/perc/sorszama, rendezett adatok szama
    BEGIN
        FOR i:=1 TO n DO
            BEGIN
                r:=0;
                FOR k:=1 TO (na[i]+nb[i]) DO
                    BEGIN
                        mo:=24;
                        mp:=60;
                        ms:=0;
                        FOR j:=1 TO (na[i]+nb[i]) DO
                            BEGIN
                                IF ((NOT(re[i,j]))AND((ko[i,j]<mo)OR((ko[i,j]=mo)AND(kp[i,j]<mp)))) THEN
                                    BEGIN
                                        mo:=ko[i,j];
                                        mp:=kp[i,j];
                                        ms:=j;
                                    END;
                            END;
                        re[i,ms]:=TRUE;
                        r:=r+1;
                        IF ms<=na[i] THEN
                            BEGIN
                                rab[i,r]:=TRUE;
                            END ELSE BEGIN
                                rab[i,r]:=FALSE;
                            END;
                        rio[i,r]:=ko[i,ms];
                        rip[i,r]:=kp[i,ms];
                        reo[i,r]:=vo[i,ms];
                        rep[i,r]:=vp[i,ms];
                    END;
            END;
    END;
 
PROCEDURE megold;
    VAR
        i,j,k,l: INTEGER;   //ciklusvalt
        aiv,biv: ARRAY[0..23,0..59] OF INTEGER; //hany szabad vonat van a/b-ben adott ora/perckor
        vane: BOOLEAN;  //talaltunk-e felhasznalhato vonatot
    BEGIN
        rendez;
        FOR i:=1 TO n DO
            BEGIN
                FOR k:=0 TO 23 DO
                    BEGIN
                        FOR l:=0 TO 59 DO
                            BEGIN
                                aiv[k,l]:=0;
                                biv[k,l]:=0;
                            END;
                    END;
                FOR j:=1 TO (na[i]+nb[i]) DO
                    BEGIN
                        vane:=FALSE;
                        IF rab[i,j] THEN
                            BEGIN
                                FOR k:=rip[i,j] DOWNTO 0 DO
                                    BEGIN
                                        IF ((aiv[rio[i,j],k]>0) AND (NOT(vane))) THEN
                                            BEGIN
                                                vane:=TRUE;
                                                aiv[rio[i,j],k]:=aiv[rio[i,j],k]-1;
                                            END;
                                    END;
                                IF rio[i,j]>1 THEN
                                    BEGIN
                                        FOR k:=rio[i,j]-1 DOWNTO 0 DO
                                            BEGIN
                                                FOR l:=59 DOWNTO 0 DO
                                                    BEGIN
                                                        IF ((aiv[k,l]>0) AND (NOT(vane))) THEN
                                                            BEGIN
                                                                vane:=TRUE;
                                                                aiv[k,l]:=aiv[k,l]-1;
                                                            END;
                                                    END;
                                            END;
                                    END;
                                IF vane THEN
                                    BEGIN
                                        IF reo[i,j]<24 THEN
                                            BEGIN
                                                biv[reo[i,j],rep[i,j]]:=biv[reo[i,j],rep[i,j]]+1;
                                            END;
                                    END ELSE BEGIN
                                        IF reo[i,j]<24 THEN
                                            BEGIN
                                                biv[reo[i,j],rep[i,j]]:=biv[reo[i,j],rep[i,j]]+1;
                                            END;
                                        av[i]:=av[i]+1;
                                    END;
                            END ELSE BEGIN
                                FOR k:=rip[i,j] DOWNTO 0 DO
                                    BEGIN
                                        IF ((biv[rio[i,j],k]>0) AND (NOT(vane))) THEN
                                            BEGIN
                                                vane:=TRUE;
                                                biv[rio[i,j],k]:=biv[rio[i,j],k]-1;
                                            END;
                                    END;
                                IF rio[i,j]>1 THEN
                                    BEGIN
                                        FOR k:=rio[i,j]-1 DOWNTO 0 DO
                                            BEGIN
                                                FOR l:=59 DOWNTO 0 DO
                                                    BEGIN
                                                        IF ((biv[k,l]>0) AND (NOT(vane))) THEN
                                                            BEGIN
                                                                vane:=TRUE;
                                                                biv[k,l]:=biv[k,l]-1;
                                                            END;
                                                    END;
                                            END;
                                    END;
                                IF vane THEN
                                    BEGIN
                                        IF reo[i,j]<24 THEN
                                            BEGIN
                                                aiv[reo[i,j],rep[i,j]]:=aiv[reo[i,j],rep[i,j]]+1;
                                            END;
                                    END ELSE BEGIN
                                        IF reo[i,j]<24 THEN
                                            BEGIN
                                                aiv[reo[i,j],rep[i,j]]:=aiv[reo[i,j],rep[i,j]]+1;
                                            END;
                                        bv[i]:=bv[i]+1;
                                    END;
                            END;
                    END;
            END;
    END;
 
PROCEDURE kiir(hova: STRING);
    VAR
        f: TEXT;
        i: INTEGER;
    BEGIN
        ASSIGN(f,hova);
        REWRITE(f);
        FOR i:=1 TO n DO
            BEGIN
                WRITELN('Case #',i,': ',av[i],' ',bv[i]);
                WRITELN(f,'Case #',i,': ',av[i],' ',bv[i]);
            END;
        CLOSE(f);
    END;
 
PROCEDURE foprogram;
    BEGIN
        beolvas('tt-large.in');
        megold;
        kiir('vasutki.txt');
    END;
    
BEGIN
    foprogram;
END.