POJ 1966--Cable TV Network【最小割 && 枚举终点起点】
2015-10-15 13:28
323 查看
[align=center]Cable TV Network[/align]
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
Sample Output
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;
}
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4276 | Accepted: 2008 |
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;
}
相关文章推荐
- NodeJS+Gulp+Browser-sync
- centOS7 安装 Nginx
- 安装PHP遇到的问题
- js Dialog 去掉右上角的X关闭功能
- java 中简单的IO流(二)
- 关于编译警告之&&
- 洛谷1349 广义斐波那契数列 【矩阵乘法】
- 国际社交壁垒下的异军突起?
- java 中简单的IO流(二)
- oracle 增加注释
- redis 使用密码登录
- 例 7-10 uva12212(迭代加深搜索)
- 例 7-10 uva12212(迭代加深搜索)
- 【codevs1975】化学方程式 dfs
- 解决Eclipse建立Maven Web项目后找不到src/main/java资源文件夹的办法
- OSW
- java 中简单的IO流(一)
- basics 最多排序
- iOS 8 AutoLayout与Size Class自悟
- 等级保护与分级保护