首页 > 代码库 > POJ-2112 Optimal Milking(最大流)未完待续~

POJ-2112 Optimal Milking(最大流)未完待续~

 

 

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 #define mem(a,b) memset(a,b,sizeof(a))
11 using namespace std;
12 typedef long long LL;
13 const int MAX=255;
14 const int oo=100000005;
15 int K,C,M,n;
16 int st,en;
17 int a[MAX][MAX],f[MAX][MAX],fa[MAX];
18 void build(int mid){
19     int i,j;
20     mem(f,0);
21     n=K+C;
22     st=0,en=n+1;
23     for (i=1;i<=K;i++)
24         f[0][i]=1;
25     for (i=K+1;i<=K+C;i++)
26         f[i][en]=M;
27     for (i=1;i<=K;i++)
28         for (j=K+1;j<=K+C;j++)
29             if (a[i][j]<=mid)
30                 f[i][j]++;
31 }
32 bool bfs(){
33     int i,j,k,u;
34     mem(fa,-1);
35     queue <int> q;
36     q.push(st);
37     fa[st]=0;
38     while (!q.empty()){
39         u=q.front();q.pop();
40         for (i=0;i<=n+1;i++){
41             if (fa[i]==-1 && f[u][i]!=0){
42                 fa[i]=fa[u]+1;
43                 q.push(i);
44             }
45         }
46     }
47     return fa[en]!=-1;
48 }
49 int dfs(int u,int curf){
50     if (u==en) return curf;
51     int dt=curf;
52     for (int v=0;v<=n+1;v++){
53         if (f[u][v]>0 && fa[v]==fa[u]+1){
54             int flow=dfs(v,min(dt,f[u][v]));
55             f[u][v]-=flow;
56             f[v][u]+=flow;
57             dt-=flow;
58         }
59     }
60     return curf-dt;
61 }
62 int dinic(){
63     int ans(0),curf;
64     while (bfs())
65         while (curf=dfs(st,oo))
66             ans+=curf;
67     return ans;
68 }
69 int main(){
70     freopen ("optimal.in","r",stdin);
71     freopen ("optimal.out","w",stdout);
72     int i,j,k;
73     while (~scanf("%d%d%d",&K,&C,&M)){
74         for (i=1;i<=K+C;i++)
75             for (j=1;j<=K+C;j++){
76                 a[i][j]=oo;
77                 int w;
78                 scanf("%d",&w);
79                 a[i][j]=min(a[i][j],w);
80             }
81         for (k=1;k<=K+C;k++)
82             for (i=1;i<=K+C;i++)
83                 for (j=1;j<=K+C;j++)
84                     a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
85         int low,high,mid;
86         low=1,high=oo;
87         while (low<=high){
88             mid=(low+high)>>1;
89             build(mid);
90             cout<<mid<< <<dinic()<<endl;
91             if (dinic()==C)
92                 high=mid-1;
93             else
94                 low=mid+1;
95         }
96         printf("%d\n",low-1);
97     }
98     return 0;
99 }

 

POJ-2112 Optimal Milking(最大流)未完待续~