您的位置:首页 > 其它

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

2015-10-15 13:28 323 查看
[align=center]Cable TV Network[/align]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: