poj 1966 zoj 2182 Cable TV Network(无向图顶点连通度(sap求最大流))

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4062 Accepted: 1902

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 0
1 0
3 3 (0,1) (0,2) (1,2)
2 0
5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)

Sample Output

0
1
3
0
2

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


同样是求关节点的个数 

这题用求最大独立轨数目  转化为最大流

求P(A, B)的方法如下:
(1) 为了求P(A, B),需要构造一个容量网络N:
1) 原图G 中的每个顶点v 变成网络N 中的2 个顶点v'和v'',顶点v'到v''有一条弧连接,即<
v', v''>,其容量为1;
2) 原图G 中的每条边e = (u, v),在网络N 中有2 条弧e' = <u'', v'>和e'' = <v'', u'>,e'和e''
的容量均为∞;
3) 另A''为源点,B'为汇点。
(2) 求从A''到B'的最大流F。
(3) 流出A''的一切弧的流量和,即为P(A, B),所有具有流量1的弧( v', v'' )对应
的顶点v 构成了一个割顶集,在图G 中去掉这些顶点后则A 和B 不再连通了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 0x3f3f3f3f
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

const int MAXN=100;//点数最大值
const int MAXM=10010;//边数的最大值
struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol=0;
    MEM(head,-1);
}
//加边  单向图三个参数 双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
    edge[tol].flow=0; head[u]=tol++;
    edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v];
    edge[tol].flow=0; head[v]=tol++;
}
//输入参数:起点 终点 点的总数
//点的编号没有影响 只要输入点的总数
int sap(int start,int end,int N)
{
    MEM(gap,0);
    MEM(dep,0);
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while(dep[start]<N)
    {
        if(u==end)
        {
            int Min=INF;
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if(Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=0;
        int v;
        for(int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=1;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])  return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start)  u=edge[pre[u]^1].to;
    }
    return ans;
}

int mp[10010][2];

int main()
{
//    fread;
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<m;i++)
            scanf(" (%d,%d)",&mp[i][0],&mp[i][1]);
        int ans=INF;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                init();
                for(int k=0;k<n;k++)
                    addedge(k,k+n,1);
                for(int k=0;k<m;k++)
                {
                    int u=mp[k][0];
                    int v=mp[k][1];
                    addedge(u+n,v,INF);
                    addedge(v+n,u,INF);
                }
                int mx=sap(i+n,j,2*n);
                ans=min(ans,mx);
            }
        }
        if(ans==INF)  ans=n;
        printf("%d\n",ans);
    }
    return 0;
}
感谢Kuangbin的模版。。。一开始用自己的那个Dinic的模版 会超时!!!!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值