POJ 1966--Cable TV Network【最小割 && 枚举终点起点】

这篇博客介绍了POJ 1966问题,涉及如何计算一个包含N个节点和M条边的无向图的稳定系数。博主分享了之前的错误建图方法,并提供了详细的解题思路。
摘要由CSDN通过智能技术生成

Cable TV Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4276 Accepted: 2008

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

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

之前做过一次,不是这样建图的,详细解析点这里 :POJ1966解析


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 500
#define maxm 22000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;
int inset, outset;
int a[maxn], b[maxn];

struct node {
    int u, v, cap, flow, next;
};

node edge[maxm];
int head[maxn], cnt;
int cur[maxn];
int dist[maxn], vis[maxn];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    node E1 = {u, v, w, 0, head[u]};
    edge[cnt] = E1;
    head[u] = cnt++;
    node E2 = {v, u, 0, 0, head[v]};
    edge[cnt] = E2;
    head[v] = cnt++;
}

void getmap(int st, int ed){
    init();
    for(int i = 1; i <= m; ++i){
        add(a[i] + n, b[i], INF);
        add(b[i] + n, a[i], INF);
    }
    for(int j = 1; j <= n; ++j){
        if(j == st || j == ed)
            add(j, j + n, INF);
        else
            add(j, j + n, 1);
    }
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0, sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u =q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flow = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        flow += DFS(st, ed, INF);
    }
    return flow;
}

void solve(){
    int num = n;
    for(int i = 1; i <= n; ++i)
    for(int j = i + 1; j <= n; ++j){
        getmap(i, j);
        num = min(num, maxflow(i, j));
    }
    printf("%d\n", num);
}

int main (){
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 1; i <= m; ++i){
            scanf(" (%d,%d)", &a[i], &b[i]);
            a[i]++, b[i]++;
        }
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值