BZOJ1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
n<=200个点m<=40000条边无向图,求 t次走不经过同条边的路径从1到n的经过的边的最大值 的最小值。
最大值最小--二分,t次不重边路径--边权1的最大流。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<algorithm> 5 //#include<iostream> 6 using namespace std; 7 8 int n,m,t; 9 #define maxn 211 10 #define maxm 160011 11 struct Edge{int to,next,v;}edge[maxm];int first[maxn],le=2; 12 void in(int x,int y,int v) {Edge &e=edge[le];e.to=y;e.v=v;e.next=first[x];first[x]=le++;} 13 void insert(int x,int y,int v) {in(x,y,v);in(y,x,v);} 14 const int inf=0x3f3f3f3f; 15 struct network 16 { 17 struct Edge{int to,next,cap,flow;}edge[maxm];int first[maxn],le,n,s,t; 18 void clear(int n) 19 { 20 memset(first,0,sizeof(first)); 21 le=2;this->n=n; 22 } 23 void in(int x,int y,int cap) {Edge &e=edge[le];e.to=y;e.cap=cap;e.flow=0;e.next=first[x];first[x]=le++;} 24 void insert(int x,int y,int cap) {in(x,y,cap);in(y,x,0);} 25 int que[maxn],head,tail,dis[maxn],cur[maxn]; 26 bool bfs() 27 { 28 memset(dis,0,sizeof(dis)); 29 dis[s]=1; 30 que[head=(tail=1)-1]=s; 31 while (head!=tail) 32 { 33 const int now=que[head++]; 34 for (int i=first[now];i;i=edge[i].next) 35 { 36 const Edge &e=edge[i]; 37 if (e.cap>e.flow && !dis[e.to]) 38 { 39 dis[e.to]=dis[now]+1; 40 que[tail++]=e.to; 41 } 42 } 43 } 44 return dis[t]; 45 } 46 int dfs(int x,int a) 47 { 48 if (x==t || !a) return a; 49 int flow=0,f; 50 for (int &i=cur[x];i;i=edge[i].next) 51 { 52 Edge &e=edge[i]; 53 if (dis[e.to]==dis[x]+1 && (f=dfs(e.to,min(a,e.cap-e.flow)))>0) 54 { 55 flow+=f; 56 e.flow+=f; 57 edge[i^1].flow-=f; 58 a-=f; 59 if (!a) break; 60 } 61 } 62 return flow; 63 } 64 int dinic(int s,int t) 65 { 66 this->s=s;this->t=t; 67 int ans=0; 68 while (bfs()) 69 { 70 for (int i=1;i<=n;i++) cur[i]=first[i]; 71 ans+=dfs(s,inf); 72 } 73 return ans; 74 } 75 }g; 76 bool check(int x) 77 { 78 g.clear(n); 79 for (int i=1;i<=n;i++) 80 for (int j=first[i];j;j=edge[j].next) 81 { 82 const Edge &e=edge[j]; 83 if (e.v<=x) g.insert(i,e.to,1); 84 } 85 return g.dinic(1,n)>=t; 86 } 87 int x,y,v; 88 int main() 89 { 90 scanf("%d%d%d",&n,&m,&t); 91 int Max=0; 92 memset(first,0,sizeof(first)); 93 for (int i=1;i<=m;i++) 94 { 95 scanf("%d%d%d",&x,&y,&v); 96 insert(x,y,v); 97 Max=max(Max,v); 98 } 99 int L=0,R=Max+1; 100 while (L<R) 101 { 102 int mid=(L+R)>>1; 103 if (check(mid)) R=mid; 104 else L=mid+1; 105 } 106 printf("%d\n",L); 107 return 0; 108 }