先二分答案,然后建分层图跑最大流,流量是人数则可行

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(int i=l;i<=r;i++)
 3 #define dec(i,l,r) for(int i=l;i>=r;i--)
 4 #define link(x) for(edge *j=h[x];j;j=j->next)
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define inf 1e9
 7 #define ll long long
 8 #define succ(x) (1<<x)
 9 #define lowbit(x) (x&(-x))
10 #define id(x,y) ((y)*n-n+(x))
11 #define NM 10000
12 #define nm 700000
13 using namespace std;
14 int read(){
15     int x=0,f=1;char ch=getchar();
16     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
17     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
18     return x*f;
19 }
20 struct edge{
21     int t,v;
22     edge *next,*rev;
23 }e[nm],*h[NM],*o,*p[NM],*tmp[NM];
24 void _add(int x,int y,int v){
25     o->t=y;o->v=v;o->next=h[x];h[x]=o++;
26 }
27 void add(int x,int y,int v){
28     _add(x,y,v);_add(y,x,0);
29     h[x]->rev=h[y];h[y]->rev=h[x];
30 }
31 int n,m,k,a[nm],b[nm],c[nm],d[NM],cnt[NM],l,r,tot;
32 bool maxflow(){
33     int flow=0;edge *j;
34     inc(i,1,tot)tmp[i]=h[i];
35     cnt[0]=tot;
36     for(int x=1,s=inf;d[x]<tot;){
37         for(j=tmp[x];j;j=j->next)
38         if(d[j->t]+1==d[x]&&j->v)break;
39         if(j){
40             s=min(s,j->v);p[j->t]=tmp[x]=j;
41             if((x=j->t)==tot){
42                 for(;x!=1;x=p[x]->rev->t)
43                 p[x]->v-=s,p[x]->rev->v+=s;
44                 flow+=s;s=inf;if(flow>=k)return true;
45             }
46         }else{
47             if(!--cnt[d[x]])break;
48             d[x]=tot;
49             link(x)if(d[j->t]+1<d[x]&&j->v)
50             d[x]=d[j->t]+1,tmp[x]=j;
51             cnt[d[x]]++;
52             if(x!=1)x=p[x]->rev->t;
53         }
54     }
55     return false;
56 }
57 bool check(int t){
58     mem(e);mem(h);o=e;mem(d);mem(p);mem(cnt);mem(tmp);tot=id(n,t+1)+1;
59     inc(i,1,t+1)add(id(n,i),tot,inf);
60     inc(i,1,n)inc(j,1,t)add(id(i,j),id(i,j+1),inf);
61     inc(i,1,m)inc(j,1,t)add(id(a[i],j),id(b[i],j+1),c[i]);
62     return maxflow();
63 }
64 int main(){
65     freopen("data.in","r",stdin);
66     n=read();m=read();k=read();
67     inc(i,1,m){
68         a[i]=read();b[i]=read();c[i]=read();
69     }
70     for(l=1,r=100;l<r;){
71         int _t=l+r>>1;
72         if(check(_t))r=_t;else l=_t+1;
73     }
74     printf("%d\n",r);
75     return 0;
76 }
View Code

 

posted on 2016-03-19 22:08  onlyRP  阅读(212)  评论(0编辑  收藏  举报