POJ 1966 Cable TV Network

POJ 1966 Cable TV Network
求无向图的连通度。
以下是我的代码:
#include < queue >
#include
< algorithm >
#include
< cstdio >
#include
< cstring >
using   namespace  std;
const   int  kMaxn( 107 );
const   int  kInf( 0x7f7f7 );

int  n,m;
bool  g[kMaxn][kMaxn];
int  source,sink,maxflow,cap[kMaxn][kMaxn],flow[kMaxn][kMaxn];

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  VertexConnectivityDegree()
{
    
int  re(kInf);
    memset(cap,
0 ,kMaxn * kMaxn * sizeof ( int ));
    
for ( int  i = 1 ;i <= n;i ++ )
        cap[i][i
+ n] = 1 ;
    
for ( int  i = 1 ;i <= n;i ++ )
        
for ( int  j = 1 ;j <= n;j ++ )
            
if (g[i][j])
                cap[i
+ n][j] = kInf;
    
for ( int  i = 1 ;i <= n;i ++ )
        
for ( int  j = 1 ;j <= n;j ++ )
            
if (i != &&   ! g[i][j])
            {
                source
= i + n;
                sink
= j;
                n
<<= 1 ;
                SAP();
                n
>>= 1 ;
                re
= min(re,maxflow);
            }
    
if (re >= kInf)
        re
= n - 1 ;
    
return  re;
}

int  main()
{
    
while (scanf( " %d%d " , & n, & m) == 2 )
    {
        memset(g,
false ,kMaxn * kMaxn * sizeof ( bool ));
        
for ( int  i = 1 ;i <= m;i ++ )
        {
            
int  u,v;
            scanf(
"  (%d,%d) " , & u, & v);
            u
++ ;v ++ ;
            g[u][v]
= g[v][u] = true ;
        }

        
int  ans(VertexConnectivityDegree());
        
if (ans >= n - 1 )
            printf(
" %d\n " ,n);
        
else
            printf(
" %d\n " ,ans);
    }

    
return   0 ;
}

你可能感兴趣的:(POJ 1966 Cable TV Network)