购买的机票限制和数据范围很容易想到是网络流
不难想到每个城市按时刻拆点,这也是一个经典模型
由于时间不会太大,我们穷举时间,不断在残留网络上建图,跑最大流
直至总流量为k即可

  1 const inf=100000007;
  2 type node=record
  3        po,next,flow:longint;
  4      end;
  5 
  6 var e:array[0..1000100] of node;
  7     a,b,c,p,numh,h,d,cur,pre:array[0..6010] of longint;
  8     s,max,t,ans,i,n,m,k,len:longint;
  9 
 10 function min(a,b:longint):longint;
 11   begin
 12     if a>b then exit(b) else exit(a);
 13   end;
 14 
 15 procedure add(x,y,f:longint);
 16   begin
 17     inc(len);
 18     e[len].po:=y;
 19     e[len].flow:=f;
 20     e[len].next:=p[x];
 21     p[x]:=len;
 22   end;
 23 
 24 procedure build(x,y,f:longint);
 25   begin
 26     add(x,y,f);
 27     add(y,x,0);
 28   end;
 29 
 30 function sap:longint;
 31   var u,i,j,tmp,neck,q:longint;
 32   begin
 33     sap:=0;
 34     u:=0;
 35     fillchar(numh,sizeof(numh),0);
 36     fillchar(h,sizeof(h),0);
 37     for i:=0 to max do
 38       cur[i]:=p[i];
 39     cur[t]:=p[t];
 40     numh[0]:=max+2;
 41     neck:=inf;
 42     while h[0]<max+2 do
 43     begin
 44       d[u]:=neck;
 45       i:=cur[u];
 46       while i<>-1 do
 47       begin
 48         j:=e[i].po;
 49         if (e[i].flow>0) and (h[u]=h[j]+1) then
 50         begin
 51           neck:=min(neck,e[i].flow);
 52           pre[j]:=u;
 53           cur[u]:=i;
 54           u:=j;
 55           if u=t then
 56           begin
 57             sap:=sap+neck;
 58             while u<>0 do
 59             begin
 60               u:=pre[u];
 61               j:=cur[u];
 62               dec(e[j].flow,neck);
 63               inc(e[j xor 1].flow,neck);
 64             end;
 65             neck:=inf;
 66           end;
 67           break;
 68         end;
 69         i:=e[i].next;
 70       end;
 71       if i=-1 then
 72       begin
 73         dec(numh[h[u]]);
 74         if numh[h[u]]=0 then break;
 75         q:=-1;
 76         tmp:=max+1;
 77         i:=p[u];
 78         while i<>-1 do
 79         begin
 80           j:=e[i].po;
 81           if (e[i].flow>0) then
 82             if h[j]<tmp then
 83             begin
 84               tmp:=h[j];
 85               q:=i;
 86             end;
 87           i:=e[i].next;
 88         end;
 89         h[u]:=tmp+1;
 90         cur[u]:=q;
 91         inc(numh[h[u]]);
 92         if u<>0 then
 93         begin
 94           u:=pre[u];
 95           neck:=d[u];
 96         end;
 97       end;
 98     end;
 99   end;
100 
101 begin
102   len:=-1;
103   fillchar(p,sizeof(p),255);
104   readln(n,m,k);
105   build(0,1,k);
106   for i:=1 to m do
107     readln(a[i],b[i],c[i]);
108   t:=6010;
109   ans:=0;
110   repeat
111     for i:=1 to m do
112       build(ans*n+a[i],(ans+1)*n+b[i],c[i]);
113     for i:=1 to n do
114       build(ans*n+i,(ans+1)*n+i,inf);
115     build((ans+1)*n+n,t,inf);
116     max:=(ans+1)*n+n;
117     inc(ans);
118     s:=s+sap;
119   until s=k;
120   writeln(ans);
121 end.
122 
123  
View Code

 

posted on 2015-02-25 18:47  acphile  阅读(143)  评论(0编辑  收藏  举报