zoukankan      html  css  js  c++  java
  • POJ 1966 Cable TV Network

    Cable TV Network
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4702   Accepted: 2173

    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.

    Source

    [Submit]   [Go Back]   [Status]   [Discuss]

    将原图中每个点拆成两个点,分别为入点和出点,从入点向出点连一条容量为1的边,代表割掉这个点的费用为1。

    对于原图中的一条边(x,y),连一条x的出点到y的入点容量为正无穷的边,以及一条y的出点到x的入点容量为正无穷的边。

    枚举新图中的S和T,S在出点中枚举,T在入点中枚举,求最小割,更新答案。

      1 #include <cstdio>
      2 #include <cstring>
      3 
      4 const int inf = 2e9;
      5 const int maxn = 5005;
      6 
      7 inline int nextInt(void)
      8 {
      9     register int ret = 0;
     10     register int neg = false;
     11     register int bit = getchar();
     12     
     13     for (; bit < 48; bit = getchar())
     14         if (bit == '-')neg ^= true;
     15         
     16     for (; bit > 47; bit = getchar())
     17         ret = ret * 10 + bit - 48;
     18         
     19     return neg ? -ret : ret;
     20 }
     21 
     22 template <class T>
     23 inline T min(T a, T b)
     24 {
     25     return a < b ? a : b;
     26 }
     27 
     28 int n, m;
     29 int s, t;
     30 int edges;
     31 int hd[maxn];
     32 int nt[maxn];
     33 int to[maxn];
     34 int fl[maxn];
     35 int bp[maxn];
     36 
     37 inline void add(int u, int v, int f)
     38 {
     39     nt[edges] = hd[u]; to[edges] = v; fl[edges] = f; hd[u] = edges++;
     40     nt[edges] = hd[v]; to[edges] = u; fl[edges] = 0; hd[v] = edges++;
     41 }
     42 
     43 int dep[maxn];
     44 
     45 inline bool bfs(void)
     46 {
     47     static int que[maxn];
     48     static int head, tail;
     49     
     50     memset(dep, 0, sizeof(dep));
     51     head = 0, tail = 0;
     52     que[tail++] = s;
     53     dep[s] = 1;
     54     
     55     while (head != tail)
     56     {
     57         int u = que[head++], v;
     58         for (int i = hd[u]; ~i; i = nt[i])
     59             if (!dep[v = to[i]] && fl[i])
     60                 dep[v] = dep[u] + 1, que[tail++] = v;
     61     }
     62     
     63     return dep[t];
     64 }
     65 
     66 int dfs(int u, int f)
     67 {
     68     if (u == t || !f)
     69         return f;
     70     
     71     int used = 0, flow, v;
     72     
     73     for (int i = hd[u]; ~i; i = nt[i])
     74         if (dep[v = to[i]] == dep[u] + 1 && fl[i])
     75         {
     76             flow = dfs(v, min(f - used, fl[i]));
     77             
     78             used += flow;
     79             fl[i] -= flow;
     80             fl[i^1] += flow;
     81             
     82             if (used == f)
     83                 return f;
     84         }
     85         
     86     if (!used)
     87         dep[u] = 0;
     88         
     89     return used;
     90 }
     91 
     92 inline int maxFlow(void)
     93 {
     94     int maxFlow = 0, newFlow;
     95     
     96     while (bfs())
     97         while (newFlow = dfs(s, inf))
     98             maxFlow += newFlow;
     99         
    100     return maxFlow;
    101 }
    102 
    103 signed main(void)
    104 {
    105     while (~scanf("%d%d", &n, &m))
    106     {
    107         memset(hd, -1, sizeof(hd)), edges = 0;
    108         
    109         for (int i = 1; i <= m; ++i)
    110         {
    111             int u = nextInt();
    112             int v = nextInt();
    113             
    114             add(u << 1, v << 1 | 1, inf);
    115             add(v << 1, u << 1 | 1, inf);
    116         }
    117         
    118         for (int i = 0; i < n; ++i)
    119             add(i << 1 | 1, i << 1, 1);
    120             
    121         memcpy(bp, fl, sizeof(bp));
    122             
    123         int ans = inf;
    124             
    125         for (int i = 0; i < n; ++i)
    126             for (int j = 0; j < n; ++j)
    127                 if (i != j)
    128                 {
    129                     s = i << 1;
    130                     t = j << 1 | 1;
    131                     memcpy(fl, bp, sizeof(fl));
    132                     ans = min(ans, maxFlow());
    133                 }
    134             
    135         printf("%d
    ", ans == inf ? n : ans);
    136     }
    137 }

    @Author: YouSiki

  • 相关阅读:
    Intellij IDEA 使用spring-boot-devtools无效解决办法一
    使用docker安装myql/redis等软件
    mybatis generator插件系列--分页插件
    mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)
    linux设置端口转发(一键设置)
    redis教程(The little redis book中文版)
    Redis 5种数据结构及其使用场景举例--STRING
    String中hashCode方法的线程安全
    java ShutdownHook介绍与使用
    ACM 模板库
  • 原文地址:https://www.cnblogs.com/yousiki/p/6229190.html
Copyright © 2011-2022 走看看