poj2455Secret Milking Machine(二分+最大流)

链接

二分距离,小于当前距离的边容量+1,使最后流>=t 

注意 会有重边

  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stdlib.h>
  6 #include<vector>
  7 #include<cmath>
  8 #include<queue>
  9 #include<set>
 10 using namespace std;
 11 #define N 205
 12 #define LL long long
 13 #define INF 0xfffffff
 14 const double eps = 1e-8;
 15 const double pi = acos(-1.0);
 16 const double inf = ~0u>>2;
 17 int path[N],flow[N],gh[N][N],st,en;
 18 int w[N][N];
 19 struct node
 20 {
 21     int u,v,c;
 22 }q[N*N];
 23 int bfs()
 24 {
 25     int i;
 26     memset(path,-1,sizeof(path));
 27     for(i = 1 ; i <= en ; i++)
 28     flow[i] = INF;
 29     queue<int>q;
 30     q.push(1);
 31     while(!q.empty())
 32     {
 33         int tk = q.front();
 34         q.pop();
 35         if(tk==en)
 36         break;
 37         for(i = 1 ; i <= en ; i++)
 38         {
 39             if(path[i]==-1&&gh[tk][i])
 40             {
 41                 path[i] = tk;
 42                 flow[i] = min(flow[tk],gh[tk][i]);
 43                 q.push(i);
 44             }
 45         }
 46     }
 47     if(path[en]==-1)
 48     return -1;
 49     return flow[en];
 50 }
 51 int EK()
 52 {
 53     int now,pre,sum=0,k;
 54     while((k=bfs())!=-1)
 55     {
 56         sum+=k;
 57         now = en;
 58         while(now!=st)
 59         {
 60             pre = path[now];
 61             gh[pre][now]-=k;
 62             gh[now][pre]+=k;
 63             now = pre;
 64         }
 65     }
 66     return sum;
 67 }
 68 int main()
 69 {
 70     int n,p,i,m;
 71     while(scanf("%d%d%d",&n,&p,&m)!=EOF)
 72     {
 73         for(i = 1; i <= p ;i++)
 74         {
 75             scanf("%d%d%d",&q[i].u,&q[i].v,&q[i].c);
 76             q[i].u++,q[i].v++;
 77         }
 78         st = 1;
 79         en = n+2;
 80         int low = 0,high = 1000000,mid;
 81         while(low<=high)
 82         {
 83             mid = (low+high)>>1;
 84             memset(gh,0,sizeof(gh));
 85             gh[st][2] = m;
 86             gh[n+1][en] = m;
 87             for(i = 1;i <= p ; i++)
 88             if(q[i].c<=mid)
 89             {
 90                 gh[q[i].u][q[i].v]+=1;
 91                 gh[q[i].v][q[i].u]+=1;
 92             }
 93             if(EK()==m)
 94             high = mid-1;
 95             else
 96             low =mid+1;
 97         }
 98         cout<<low<<endl;
 99     }
100     return 0;
101 }
View Code

 

posted @ 2014-05-06 13:51  _雨  阅读(185)  评论(0编辑  收藏  举报