zoukankan      html  css  js  c++  java
  • ZOJ 2532 Internship(最大流找关键割边)

    Description

    CIA headquarter collects data from across the country through its classified network. They have been using optical fibres long before it's been deployed on any civilian projects. However they are still under a lot pressure recently because the data are growing rapidly. As a result they are considering upgrading the network with new technologies that provide a few times wider bandwidth. In the experiemental stage, they would like to upgrade one segment of their original network in order to see how it performs. And as a CIA intern it's your responsibility to investigate which segment could actually help increase the total bandwidth the headquarter receives, suppose that all the cities have infinite data to send and the routing algorithm is optimized. As they have prepared the data for you in a few minutes, you are told that they need the result immediately. Well, practically immediately.

    Input

    Input contains multiple test cases. First line of each test case contains three integers n, m and l, they represent the number of cities, the number of relay stations and the number of segments. Cities will be referred to as integers from 1 to n, while relay stations use integers from n+1 to n+m. You can saves assume that n + m <= 100, l <= 1000 (all of them are positive). The headquarter is identified by the integer 0.

    The next l lines hold a segment on each line in the form of a b c, where a is the source node and b is the target node, while c is its bandwidth. They are all integers where a and b are valid identifiers (from 0 to n+m). c is positive. For some reason the data links are all directional.

    The input is terminated by a test case with n = 0. You can safely assume that your calculation can be housed within 32-bit integers.

    Output

    For each test print the segment id's that meets the criteria. The result is printed in a single line and sorted in ascending order, with a single space as the separator. If none of the segment meets the criteria, just print an empty line. The segment id is 1 based not 0 based.

    题目大意:有n个发射点,m个中间点,和一个接受点0,中间由L条有向光纤连接,每个光纤有一定的带宽,问扩大哪些光纤(只能扩大一条)的带宽可以扩大发射点的总带宽。

    思路:从源点S到n个发射点连一条容量为无穷大的边,再连接L条光纤,容量为该光纤的带宽,以0为汇点T。题目转化成扩大哪些边的容量可以增加最大流。先在图上求最大流,在残量网络中,把S能到的点标记为1,把能到T的点标记为2,那么当一条边的出发点为1、结束点为2,这条边就是关键割边,也就是所求的边之一。如果有一条边的其中一个点未被标记,那么扩大这条边的容量,也就不过是从源点到达的点多了一个,或者到达汇点的点多了一个,但是在这个残量网络中依旧没有从S到T的增广路。如果两个点都未被标记……那更加不可以啦……

    PS:出发点为2结束点为1的边,增加容量也不会有增广路出现,曾因为这个WA了一次>_<

    BFS+ISAP(30MS):

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 using namespace std;
      6 
      7 const int MAXN = 110;
      8 const int MAXE = 3010;
      9 const int INF = 0x3fff3fff;
     10 
     11 struct SAP {
     12     int head[MAXN], dis[MAXN], pre[MAXN], cur[MAXN], gap[MAXN];
     13     int to[MAXE], next[MAXE], flow[MAXE];
     14     int n, st, ed, ecnt;
     15 
     16     void init() {
     17         memset(head, 0, sizeof(head));
     18         ecnt = 2;
     19     }
     20 
     21     void add_edge(int u, int v, int c) {
     22         to[ecnt] = v; flow[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
     23         to[ecnt] = u; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
     24         //printf("%d->%d flow = %d
    ", u, v, c);
     25     }
     26 
     27     void bfs() {
     28         memset(dis, 0x3f, sizeof(dis));
     29         queue<int> que; que.push(ed);
     30         dis[ed] = 0;
     31         while(!que.empty()) {
     32             int u = que.front(); que.pop();
     33             ++gap[dis[u]];
     34             for(int p = head[u]; p; p = next[p]) {
     35                 int &v = to[p];
     36                 if(flow[p ^ 1] && dis[v] > n) {
     37                     dis[v] = dis[u] + 1;
     38                     que.push(v);
     39                 }
     40             }
     41         }
     42     }
     43 
     44     int Max_flow(int ss, int tt, int nn) {
     45         st = ss; ed = tt; n = nn;
     46         int ans = 0, minFlow = INF, u;
     47         for(int i = 0; i <= n; ++i) {
     48             cur[i] = head[i];
     49             gap[i] = 0;
     50         }
     51         u = pre[st] = st;
     52         bfs();
     53         while(dis[st] < n) {
     54             bool flag = false;
     55             for(int &p = cur[u]; p; p = next[p]) {
     56                 int &v = to[p];
     57                 if(flow[p] && dis[u] == dis[v] + 1) {
     58                     flag = true;
     59                     minFlow = min(minFlow, flow[p]);
     60                     pre[v] = u;
     61                     u = v;
     62                     if(u == ed) {
     63                         ans += minFlow;
     64                         while(u != st) {
     65                             u = pre[u];
     66                             flow[cur[u]] -= minFlow;
     67                             flow[cur[u] ^ 1] += minFlow;
     68                         }
     69                         minFlow = INF;
     70                     }
     71                     break;
     72                 }
     73             }
     74             if(flag) continue;
     75             int minDis = n - 1;
     76             for(int p = head[u]; p; p = next[p]) {
     77                 int &v = to[p];
     78                 if(flow[p] && minDis > dis[v]) {
     79                     minDis = dis[v];
     80                     cur[u] = p;
     81                 }
     82             }
     83             if(--gap[dis[u]] == 0) break;
     84             gap[dis[u] = minDis + 1]++;
     85             u = pre[u];
     86         }
     87         return ans;
     88     }
     89 
     90     int mark[MAXN];
     91 
     92     void make_cut() {
     93         memset(mark, 0, sizeof(mark));
     94         queue<int> que;
     95         que.push(st); mark[st] = 1;
     96         while(!que.empty()) {
     97             int u = que.front(); que.pop();
     98             for(int p = head[u]; p; p = next[p]) {
     99                 int &v = to[p];
    100                 if(flow[p] && !mark[v]) {
    101                     mark[v] = 1;
    102                     que.push(v);
    103                 }
    104             }
    105         }
    106         que.push(ed); mark[ed] = 2;
    107         while(!que.empty()) {
    108             int u = que.front(); que.pop();
    109             for(int p = head[u]; p; p = next[p]) {
    110                 int &v = to[p];
    111                 if(flow[p ^ 1] && !mark[v]) {
    112                     mark[v] = 2;
    113                     que.push(v);
    114                 }
    115             }
    116         }
    117     }
    118 } G;
    119 
    120 int from[MAXE], to[MAXE];
    121 int n, m, L, c;
    122 
    123 int main() {
    124     while(scanf("%d%d%d", &n, &m, &L) != EOF) {
    125         if(n == 0) break;
    126         int ss = n + m + 1, tt = 0;
    127         G.init();
    128         for(int i = 1; i <= n; ++i) G.add_edge(ss, i, INF);
    129         for(int i = 1; i <= L; ++i) {
    130             scanf("%d%d%d", &from[i], &to[i], &c);
    131             G.add_edge(from[i], to[i], c);
    132         }
    133         //printf("%d
    ", G.Max_flow(ss, tt, ss));
    134         G.Max_flow(ss, tt, ss);
    135         G.make_cut();
    136         bool flag = false;
    137         for(int i = 1; i <= L; ++i) {
    138             int &u = from[i], &v = to[i];
    139             if(G.mark[u] == 1 && G.mark[v] == 2) {
    140                 if(flag) printf(" ");
    141                 printf("%d", i);
    142                 flag = true;
    143             }
    144         }
    145         puts("");
    146     }
    147 }
    View Code
  • 相关阅读:
    CXF JaxWsDynamicClientFactory 错误:编码GBK的不可映射字符
    关于springboot配置DataSource
    Spring Boot2.0加载多个数据源
    Kettle配置发送邮件
    推荐几个不错的VUE UI框架
    vue基础语法一
    Maven在Eclipse下构建多模块项目过程
    利用eclipse把jar包安装到本地仓库
    设计模式之策略模式
    设计模式之观察者模式
  • 原文地址:https://www.cnblogs.com/oyking/p/3247032.html
Copyright © 2011-2022 走看看