码迷,mamicode.com
首页 > Web开发 > 详细

POJ 1966 Cable TV Network(无向图的顶点连通度)

时间:2014-08-31 18:43:01      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:图的连通性   图论   poj   无向图的顶点连通度   

POJ 1966 Cable TV Network

链接:http://poj.org/problem?id=1966


题意:有线电视网络中,中继器的连接是双向的。如果网络中任何两个中继器之间至少有一条路,则中继器网络称为是连通的,否则中继器网络是不连通的。一个空的网络、以及只有一个中继器的网络被认为是连通的。具有n 个中继器的网络的安全系数f 被定义成:
(1) f 为n,如果不管删除多少个中继器,剩下的网络仍然是连通的;
(2) f 为删除最少的顶点数,使得剩下的网络不连通。

现在给定一个有线电视网络,求 f 为多少。


思路:一张连通图,求最少删去多少个点,能够使得该图不再连通,这是无向图的顶点连通度问题。

无向图的点连通度:
        1. 将每个点u拆为u‘, u‘‘.顶点u‘到u‘‘连一条弧,容量为1。
        2. 将图中的每条边(u, v)拆成<u‘‘, v‘>和<v‘‘, u‘>两条边,每条边的容量为INF。
        3. 选一个源点A‘‘, 枚举汇点B‘. 求出最大流的最小值即为点连通度。
        4. 所有具有流量1的弧(v‘, v‘‘)对应的顶点v构成了一个割点集。


代码:

/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-6
#define debug puts("===============")
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)
typedef long long ll;
typedef unsigned long long ULL;
const int maxn = 1000;
const int maxm = 100000;
int edge[maxm][2];
struct node {
    int v;    // vertex
    int cap;    // capacity
    int flow;   // current flow in this arc
    int nxt;
} e[maxm * 2];
int g[maxn], cnt;
int st, ed, n, m;
void add(int u, int v, int c) {
    e[++cnt].v = v;
    e[cnt].cap = c;
    e[cnt].flow = 0;
    e[cnt].nxt = g[u];
    g[u] = cnt;

    e[++cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].flow = 0;
    e[cnt].nxt = g[v];
    g[v] = cnt;
}

void init(int N) {
    cnt = 1;
    st = N;
    for (int i = 0; i < n; i++) g[i] = 0;
    int u, v;
    for (int i = 0; i < N; i++) add(i, i + N, 1);
    for (int i = 0; i < m; i++) {
        int u = edge[i][0], v = edge[i][1];
        add(v + N, u, INF);
        add(u + N, v, INF);
    }
}

int dist[maxn], numbs[maxn], q[maxn];
void rev_bfs() {
    int font = 0, rear = 1;
    for (int i = 0; i <= n; i++) { //n为总点数
        dist[i] = maxn;
        numbs[i] = 0;
    }
    q[font] = ed;
    dist[ed] = 0;
    numbs[0] = 1;
    while(font != rear) {
        int u = q[font++];
        for (int i = g[u]; i; i = e[i].nxt) {
            if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue;
            dist[e[i].v] = dist[u] + 1;
            ++numbs[dist[e[i].v]];
            q[rear++] = e[i].v;
        }
    }
}
int maxflow() {
    rev_bfs();
    int u, totalflow = 0;
    int curg[maxn], revpath[maxn];
    for(int i = 0; i <= n; ++i) curg[i] = g[i];
    u = st;
    while(dist[st] < n) {
        if(u == ed) {   // find an augmenting path
            int augflow = INF;
            for(int i = st; i != ed; i = e[curg[i]].v)
                augflow = min(augflow, e[curg[i]].cap);
            for(int i = st; i != ed; i = e[curg[i]].v) {
                e[curg[i]].cap -= augflow;
                e[curg[i] ^ 1].cap += augflow;
                e[curg[i]].flow += augflow;
                e[curg[i] ^ 1].flow -= augflow;
            }
            totalflow += augflow;
            u = st;
        }
        int i;
        for(i = curg[u]; i; i = e[i].nxt)
            if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break;
        if(i) {   // find an admissible arc, then Advance
            curg[u] = i;
            revpath[e[i].v] = i ^ 1;
            u = e[i].v;
        } else {    // no admissible arc, then relabel this vertex
            if(0 == (--numbs[dist[u]])) break;    // GAP cut, Important!
            curg[u] = g[u];
            int mindist = n;
            for(int j = g[u]; j; j = e[j].nxt)
                if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]);
            dist[u] = mindist + 1;
            ++numbs[dist[u]];
            if(u != st)
                u = e[revpath[u]].v;    // Backtrack
        }
    }
    return totalflow;
}
int main () {
    int N;
    while(~scanf("%d%d", &N, &m)) {
        n = 2 * N + 4;
        for (int i = 0; i < m; i++) scanf(" (%d,%d)", edge[i], edge[i] + 1);
        int ans = INF;
        for (int i = 1; i < N; i++) {
            ed = i;
            init(N);
            ans = min(ans, maxflow());
        }
        if (ans == INF) ans = N;
        printf("%d\n", ans);
    }
    return 0;
}


POJ 1966 Cable TV Network(无向图的顶点连通度)

标签:图的连通性   图论   poj   无向图的顶点连通度   

原文地址:http://blog.csdn.net/sio__five/article/details/38961679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!