POJ 1698 Alive's Chance

Program poj1698;

const fiftyweeks=350;

type cord=record

             po,da,ne:longint;

          end;

 

var tt,i,n,le,s,t,flow,tot:longint;

    a:array[1..9]of longint;

    head:array[1..500]of longint;

    link:array[-100000..100000]of cord;

    cur,dis:array[1..500]of longint;

    vh:array[0..500]of longint;

 

 Procedure add(x,y,z:longint);

  Begin

     inc(le);

     with link[le] do

      begin

         po:=y;da:=z;ne:=head[x];

      end;

     head[x]:=le;

     with link[-le] do

      begin

         po:=x;da:=0;ne:=head[y];

      end;

     head[y]:=-le;

  end;

 

 Procedure init;

 var i,j,k:longint;

  Begin

     le:=0;

     flow:=0;tot:=0;

     readln(n);

     s:=fiftyweeks+n+1;t:=fiftyweeks+n+2;

     fillchar(head,sizeof(head),0);

     for i:=1 to n do

      Begin

        for j:=1 to 9 do read(a[j]);

        add(s,i+fiftyweeks,a[8]);

        inc(tot,a[8]);

         for k:=1 to 7 do

          if a[k]=1 then

           for j:=1 to a[9] do add(i+fiftyweeks,(j-1)*7+k,1);

      end;

     for i:=1 to fiftyweeks do

       add(i,t,1);

  end;

 

 Function min(a,b:longint):longint;

  begin

    if a<b then min:=a else min:=b;

  end;

 

 Function aug(x,nf:longint):longint;

 var i,j,l,d,minh,ins:longint;

  begin

     if x=t then exit(nf);

     l:=nf;

     i:=cur[x];

     while i<>0 do

      begin

         if (link[i].da>0)and(dis[link[i].po]+1=dis[x]) then

              begin

                 cur[x]:=i;

                 d:=aug(link[i].po,min(l,link[i].da));

                 dec(link[i].da,d);

                 inc(link[-i].da,d);

                 dec(l,d);

                 if (dis[s]=t)or(l=0) then exit(nf-l);

              end;

         i:=link[i].ne;

      end;

     if l=nf then

      begin

         i:=head[x];

         minh:=t;

         while i<>0 do

          begin

            if (link[i].da>0)and(dis[link[i].po]<minh) then

                        begin

                           minh:=dis[link[i].po];

                           ins:=i;

                        end;

            i:=link[i].ne;

          end;

         cur[x]:=ins;

         dec(vh[dis[x]]);

         if vh[dis[x]]=0 then dis[s]:=t;

         dis[x]:=minh+1;

         inc(vh[dis[x]]);

      end;

     aug:=nf-l;

  end;

 

 Procedure main;

 var i,j,k:longint;

  Begin

     fillchar(dis,sizeof(dis),0);

     for i:=1 to t do cur[i]:=head[i];

     fillchar(vh,sizeof(vh),0);

     vh[0]:=t;

     while dis[s]<t do inc(flow,aug(s,maxlongint));

     if flow=tot then writeln('Yes') else writeln('No');

  end;

 

Begin

   assign(input,'input.in');assign(output,'output.out');

   reset(input);rewrite(output);

     readln(tt);

     for i:=1 to tt do

      begin

         init;

         main;

      end;

   close(input);close(output);

End.

posted on 2016-03-02 20:54  Yesphet  阅读(171)  评论(0编辑  收藏  举报