POJ 2112 Optimal Milking

POJ 2112 Optimal Milking
第一次1Y网络流构图的题目。
题目要求最大路径最小,那么不妨让每个奶牛都按照最短路径到达(走了最大路径的奶牛一定是按照最短路到达),这样就可以把一条路径看成从挤奶器到奶牛的一条边,边权为最短路长度。
最大路径不知道是多少,可以使用二分,因为最大路径越大,图中有效的边越多。就是要找到一个最小的最大路径,使得建立的网络流的最大流依然为奶牛总数。
以下是我的代码:
#include < queue >
#include
< algorithm >
#include
< cstdio >
#include
< cstring >
using   namespace  std;
const   int  kMaxn( 237 );
const   int  kInf( 0x7f7f7f7f );

int  k,c,m,maxdist,dist[kMaxn][kMaxn];
int  n,source,sink,maxflow,cap[kMaxn][kMaxn],flow[kMaxn][kMaxn];

void  Floyed()
{
    
for ( int  t = 1 ;t <= k + c;t ++ )
        
for ( int  i = 1 ;i <= k + c;i ++ )
            
for ( int  j = 1 ;j <= k + c;j ++ )
                
if (dist[i][t] < kInf  &&  dist[t][j] < kInf  &&  dist[i][j] > dist[i][t] + dist[t][j])
                    dist[i][j]
= dist[i][t] + dist[t][j];
}

void  SAP()
{
    
int  d[kMaxn],num[kMaxn],p[kMaxn];

    memset(flow,
0 ,kMaxn * kMaxn * sizeof ( int ));
    memset(d,
0x7f ,kMaxn * sizeof ( int ));
    memset(num,
0 ,kMaxn * sizeof ( int ));
    queue
< int >  q;
    d[sink]
= 0 ;
    num[
0 ] ++ ;
    q.push(sink);
    
while ( ! q.empty())
    {
        
int  v(q.front());q.pop();
        
for ( int  u = 1 ;u <= n;u ++ )
            
if (d[u] >= &&  cap[u][v] > 0 )
            {
                d[u]
= d[v] + 1 ;
                num[d[u]]
++ ;
                q.push(u);
            }
    }

    maxflow
= 0 ;
    
int  u(source),v;
    
while (d[source] < n)
    {
        v
=- 1 ;
        
for ( int  i = 1 ;i <= n;i ++ )
            
if (cap[u][i] > flow[u][i]  &&  d[u] == d[i] + 1 )
            {
                v
= i; break ;
            }
        
if (v !=- 1 )
        {
            p[v]
= u;u = v;
            
if (u == sink)
            {
                
int  add(kInf);
                
for (u = sink;u != source;u = p[u])
                    add
= min(add,cap[p[u]][u] - flow[p[u]][u]);
                maxflow
+= add;
                
for (u = sink;u != source;u = p[u])
                {
                    flow[p[u]][u]
+= add;
                    flow[u][p[u]]
-= add;
                }
            }
        }
        
else
        {
            num[d[u]]
-- ;
            
if ( ! num[d[u]])
                
return ;
            d[u]
= n;
            
for (v = 1 ;v <= n;v ++ )
                
if (cap[u][v] > flow[u][v])
                    d[u]
= min(d[u],d[v] + 1 );
            num[d[u]]
++ ;
            
if (u != source)
                u
= p[u];
        }
    }
}

int  binary( int  l, int  r)
{
    
while (l < r)
    {
        
int  mid((l + r) >> 1 );

        source
= k + c + 1 ;
        sink
= k + c + 2 ;
        n
= k + c + 2 ;
        memset(cap,
0 ,kMaxn * kMaxn * sizeof ( int ));
        
for ( int  i = 1 ;i <= k;i ++ )
            cap[source][i]
= m;
        
for ( int  i = k + 1 ;i <= k + c;i ++ )
            cap[i][sink]
++ ;
        
for ( int  i = 1 ;i <= k;i ++ )
            
for ( int  j = k + 1 ;j <= k + c;j ++ )
                cap[i][j]
= (dist[i][j] <= mid);

        SAP();

        
if (maxflow < c)
            l
= mid + 1 ;
        
else
            r
= mid;
    }
    
return  l;
}

int  main()
{
    memset(dist,
0x7f ,kMaxn * kMaxn * sizeof ( int ));
    scanf(
" %d%d%d " , & k, & c, & m);
    
for ( int  i = 1 ;i <= k + c;i ++ )
        
for ( int  j = 1 ;j <= k + c;j ++ )
        {
            
int  t;
            scanf(
" %d " , & t);
            
if (t == 0 )
                t
= kInf;
            dist[i][j]
= t;
        }

    Floyed();
    maxdist
= 0 ;
    
for ( int  i = 1 ;i <= k + c;i ++ )
        
for ( int  j = 1 ;j <= k + c;j ++ )
            
if (dist[i][j] < kInf  &&  maxdist < dist[i][j])
                maxdist
= dist[i][j];

    printf(
" %d\n " ,binary( 1 ,maxdist + 1 ));

    
return   0 ;
}

你可能感兴趣的:(POJ 2112 Optimal Milking)