约定:
2 <= N <= 50
1 <= M <= 2450
1 <= T <= 50
1 <= X,Y <= N
X != Y
1 <= Z <= 50
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #define maxn 10010 7 #define maxm 60000 8 #define inf 1061109567 9 using namespace std; 10 char ch; 11 bool ok; 12 void read(int &x){ 13 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1; 14 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar()); 15 if (ok) x=-x; 16 } 17 int n,m,t,u[maxm],v[maxm],c[maxm],l,r,mid; 18 struct flow{ 19 int s,t,tot,now[maxn],son[maxm],pre[maxm],val[maxm]; 20 int dis[maxn],head,tail,list[maxn]; 21 bool bo[maxn]; 22 void init(){s=0,t=maxn-1,tot=1,memset(now,0,sizeof(now));} 23 void put(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;} 24 void add(int a,int b,int c){put(a,b,c),put(b,a,0);} 25 bool bfs(){ 26 memset(bo,0,sizeof(bo)); 27 head=0,tail=1,list[1]=s,dis[s]=0,bo[s]=1; 28 while (head<tail){ 29 int u=list[++head]; 30 for (int p=now[u],v=son[p];p;p=pre[p],v=son[p]) 31 if (val[p]&&!bo[v]) bo[v]=1,dis[v]=dis[u]+1,list[++tail]=v; 32 } 33 return bo[t]; 34 } 35 int dfs(int u,int rest){ 36 if (u==t) return rest; 37 int ans=0; 38 for (int p=now[u],v=son[p];p&&rest;p=pre[p],v=son[p]) 39 if (val[p]&&dis[v]==dis[u]+1){ 40 int d=dfs(v,min(rest,val[p])); 41 val[p]-=d,val[p^1]+=d,ans+=d,rest-=d; 42 } 43 if (!ans) dis[u]=-1; 44 return ans; 45 } 46 int dinic(){ 47 int ans=0; 48 while (bfs()) ans+=dfs(s,inf); 49 return ans; 50 } 51 }f; 52 bool check(int lim){ 53 f.init(); 54 f.add(f.s,1,t); 55 for (int i=1;i<=m;i++) for (int j=1;j<=lim;j++) f.add(u[i]+(j-1)*n,v[i]+j*n,c[i]); 56 for (int i=1;i<n;i++) for (int j=1;j<=lim;j++) f.add(i+(j-1)*n,i+j*n,inf); 57 for (int i=1;i<=lim+1;i++) f.add(i*n,f.t,inf); 58 return f.dinic()==t; 59 } 60 int main(){ 61 read(n),read(m),read(t); 62 for (int i=1;i<=m;i++) read(u[i]),read(v[i]),read(c[i]); 63 for (l=1,r=n+t,mid=(l+r)>>1;l<r;mid=(l+r)>>1) if (check(mid)) r=mid; else l=mid+1; 64 printf("%d\n",l); 65 return 0; 66 }
bzoj1570: [JSOI2008]Blue Mary的旅行
原文:http://www.cnblogs.com/chenyushuo/p/5138373.html