poj 1966 Cable TV Network 【枚举源汇 求解 无向图最小割】

Cable TV Network
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4269   Accepted: 2004

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. 
poj 1966 Cable TV Network 【枚举源汇 求解 无向图最小割】_第1张图片

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.

定义一个N个点M条边的无向图的稳定系数f
一、去掉图中所有点都不能使图不连通,则f = N;
二、最小去掉ans个点就可以使图不连通,则f = ans;

题意:给你一个N个点M条边的无向图,问你这个图的稳定系数。

很裸的题目了,最多不过50个点,直接暴力最小割即可。

思路:枚举任意的点对i和j,拆点建图后求解i -> j + N的最小割,每次更新较小的最小割。建图时把源点和汇点拆成的边赋值为无穷大,其余点拆成的边赋值为1。



AC代码:

//POJ1966
#include 
#include 
#include 
#include 
#define MAXN 100+10
#define MAXM 20000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM], Redge[MAXM];
int head[MAXN], Rhead[MAXN], edgenum;
int N, M;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        if(edge[i].to == v)
            return ;
    }
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
int a[MAXM], b[MAXM];
void input()
{
    for(int i = 0; i < M; i++)
        scanf(" (%d,%d)", &a[i], &b[i]), a[i]++, b[i]++;
}
void getMap(int s, int t)
{
    init();
    for(int i = 0; i < M; i++)
    {
        addEdge(a[i]+N, b[i], INF);
        addEdge(b[i]+N, a[i], INF);
    }
    for(int k = 1; k <= N; k++)
        (k != s && k != t) ? addEdge(k, k+N, 1) : addEdge(k, k+N, INF);
}
bool BFS(int s, int t)
{
    queue Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0; vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                if(E.to == t) return true;
                vis[E.to] = true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
        {
            edge[i].flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(s, INF, t);
    }
    return flow;
}
void solve()
{
    int ans = N;
    for(int i = 1; i <= N; i++)//枚举源点
    {
        for(int j = i+1; j <= N; j++)//汇点
        {
            getMap(i, j);
            ans = min(ans, Maxflow(i, j));//更新
        }
    }
    printf("%d\n", ans);
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        input();
        solve();
    }
    return 0;
}


你可能感兴趣的:(网络流)