心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
求无向图的连通度。
以下是我的代码:
#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;
}
posted on 2011-06-01 22:33 lee1r 阅读(249) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:图论

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理