【poj-1966】 Cable TV Network(最小割+dinic)

描述

传送门:poj-1966

 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.

Examples

  • intput

    1
    2
    3
    4
    5
    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)
  • output

    1
    2
    3
    4
    5
    0
    1
    3
    0
    2

大致题意

给定一个图,求至少去掉多少个点可以使得图不联通。

思路

  • 这道题是割点不是割边,所以需要把点拆成两个点,然后在两个点之间连一条长为1的边,这样切割这条边就相当于去掉这个点。
  • 同样的,题目中原本给定的边是不能被切的,那么我们把这些边的权值都赋值为inf(很大的数),这样就不会切掉这些边。
  • 枚举源点和汇点。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<queue>
#include<string.h>
#include<cmath>
using namespace std;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define repd(i,a,n) for(int i=n;i>=a;i--)
#define CRL(a,x) memset(a,x,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1e2+5;
const double Pi = acos(-1);
const double e = exp(1.0);
const int mod=1e9+7;

int Begin[N],cur[N],d[N],cnt,s,t;
struct node{int to,c,f,next;}Edge[10005];

void add(int u,int v,int w){
Edge[++cnt]=node{v,w,0,Begin[u]};
Begin[u]=cnt;
Edge[++cnt]=node{u,0,0,Begin[v]};
Begin[v]=cnt;
}

bool bfs(){
queue<int>Q;Q.push(s);
CRL(d,0);d[s]=1;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=Begin[u];~i;i=Edge[i].next){
int v=Edge[i].to;
if(Edge[i].c>Edge[i].f&&!d[v]){
d[v]=d[u]+1;
Q.push(v);
if(v==t) return true;
}
}
}
return false;
}

int dfs(int u,int flow){
if(u==t) return flow;
for(int &i=cur[u];~i;i=Edge[i].next){
int v=Edge[i].to,tflow;
if(Edge[i].c>Edge[i].f&&d[v]==d[u]+1){
tflow=dfs(v,min(flow,Edge[i].c-Edge[i].f));
if(tflow){
Edge[i].f+=tflow;
Edge[i^1].f-=tflow;
return tflow;
}
}
}
return 0;
}

int dinic(int n){
int maxflow=0,tflow;
while(bfs()){
rep(i,0,2*n-1) cur[i]=Begin[i];
while(tflow=dfs(s,inf))
maxflow+=tflow;
}
return maxflow;
}

int main(){
int n,m,u,v;
while(~scanf("%d%d",&n,&m)){
CRL(Begin,-1);cnt=-1;
s=n;int ans=inf;
rep(i,0,n-1) add(i,i+n,1); //拆点

rep(i,1,m){
scanf(" (%d,%d)",&u,&v);
add(u+n,v,inf); //连很大的边
add(v+n,u,inf);
}

for(s=n;s<n*2;s++)
for(t=0;t<n;t++){
if(t+n==s) continue;
ans=min(dinic(n),ans);
for(int i=0;i<cnt;i+=2)
Edge[i].f= Edge[i^1].f=0;
}

printf("%d\n",ans==inf? n:ans);
}
return 0;
}
-------------本文结束 感谢您的阅读-------------

本文标题:【poj-1966】 Cable TV Network(最小割+dinic)

文章作者:Armin

发布时间:2019年08月11日 - 15:08

最后更新:2019年08月11日 - 15:08

原始链接:http://x-armin.com/Cable-TV-Network/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

小礼物走一波