poj1966Cable TV Network【顶点连通度=>最小割】

来源:互联网 发布:遇见未来js 编辑:程序博客网 时间:2024/04/29 16:56

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 4588 Accepted: 2131

Description

The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor f of a network with n relays is:
1. n, if the net remains connected regardless the number of relays removed from the net.
2. The minimal number of relays that disconnect the network when removed.

For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.

Input

Write a program that reads several data sets from the standard input and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two integers: 0<=n<=50,the number of relays in the net, and m, the number of cables in the net. Follow m data pairs (u,v), u < v, where u and v are relay identifiers (integers in the range 0..n-1). The pair (u,v) designates the cable that interconnects the relays u and v. The pairs may occur in any order.Except the (u,v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net.

Sample Input

0 01 03 3 (0,1) (0,2) (1,2)2 05 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)

Sample Output

01302

Hint

The first data set encodes an empty network, the second data set corresponds to a network with a single relay, and the following three data sets encode the nets shown in figure 1.

Source

Southeastern Europe 2004

题意:给出一个n个节点和m条边的图,求该图的顶点连通度。

分析: 顶点连通度的求解可以转换为网络最大流问题。

(1)原图G中的每个顶点v变成网络中的两个顶点v‘和v’‘,顶点v’至v''有一个条弧(有向边)连接,弧容量为1;

(2)原图G中的每条边e=uv,在网络中有两条弧e'=u''v',e''=v''u'与之对应,e'弧容量为oo(无穷) ,e''弧容量为oo(无穷)

(3)A''为源点,B'为汇点,枚举所有汇点,求最小割最小的那个

n*n枚举的时间复杂度就说的过去,毕竟数据少

初始化的时候要注意最大值是n!!!!总共就只有n个点啊,还有,加正常的无穷边时是双向的!!!!

    /**********    poj1966    2016.7.29    204K32MSC++3518B    ***********/    #include <iostream>    #include<cstdio>    #include<cstring>    using namespace std;    const int mm=1000000;    const int mn=505*505*3;    const int oo=1000000000;    int node,src,dest,edge;    int reach[mm],flow[mm],nxt[mm];    int head[mn],work[mn],dis[mn],q[mn];    inline int min(int a,int b)    {        return a<b?a:b;    }    inline void prepare(int _node,int _src,int _dest)    {        node=_node,src=_src,dest=_dest;        for(int i=0;i<node;++i)head[i]=-1;        edge=0;    }    inline void addedge(int u,int v,int c1,int c2=0)    {        reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;        reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;    }    bool Dinic_bfs()    {        int i,u,v,l,r=0;        for(i=0;i<node;++i)dis[i]=-1;        dis[q[r++]=src]=0;        for(l=0;l<r;++l)            for(i=head[u=q[l]];i>=0;i=nxt[i])                if(flow[i]&&dis[v=reach[i]]<0)                {                    dis[q[r++]=v]=dis[u]+1;                    if(v==dest)return 1;                }        return 0;    }    int Dinic_dfs(int u,int exp)    {        if(u==dest)return exp;        for(int &i=work[u],v,tmp;i>=0;i=nxt[i])            if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)            {                flow[i]-=tmp;                flow[i^1]+=tmp;                return tmp;            }dis[u]--;        return 0;    }    int Dinic_flow()    {        int i,ret=0,delta;        while(Dinic_bfs())        {            for(i=0;i<node;++i)work[i]=head[i];            while(delta=Dinic_dfs(src,oo))ret+=delta;        }        return ret;    }    struct edge    {        int u,v;    }num[100000];    int main()    {       // freopen("cin.txt","r",stdin);        int n,m;        while(~scanf("%d%d",&n,&m))        {            if(m==0)            {                if(n==1)printf("1\n");                else printf("0\n");                continue;            }            for(int i=0;i<m;i++)            {                char str[30];                scanf("%s",str);              //  printf("str=%s\n",str);                int len=strlen(str);              //  printf("len=%d\n",len);                int a=0,j=1;                for(;str[j]!=',';j++)a=a*10+str[j]-'0';               // printf("u=%d    ",a);                num[i].u=a;                int b=0;                j++;                for(;')'!=str[j];j++)b=b*10+str[j]-'0';                num[i].v=b;              //  printf("i=%d,u=%d,v=%d\n",j,num[i].u,num[i].v);            }            int minn=n;            for(int i=0;i<n;i++)            {                for(int j=i+1;j<n;j++)                {                    prepare(2*n,i,j);                    for(int k=0;k<m;k++)                    {                        addedge(num[k].u+n,num[k].v,oo);                        addedge(num[k].v+n,num[k].u,oo);                    }                        //printf("u=%d,v=%d\n",num[k].u,num[k].v);                    for(int k=0;k<n;k++)                    {                        if(k!=i&&k!=j)                            addedge(k,k+n,1);                        else                            addedge(k,k+n,oo);                    }                    int tmp=Dinic_flow();                  //  printf("tmp=%d\n",tmp);                    if(tmp<minn)minn=tmp;                }            }            printf("%d\n",minn);        }        return 0;    }


0 0
原创粉丝点击