zoukankan      html  css  js  c++  java
  • ZOJ 3229 Shoot the Bullet(有源汇的上下界最大流)

    Description

    Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies, youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!

    During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at least Gx photos of girl x in total in the Bunkachou. At the k-th day, there are Ck targets, Tk1, Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [Lki, Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use her last spell card to attack Aya. What's more, Aya cannot take more than Dk photos at the k-th day. Under these constraints, the more photos, the better.

    Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

    Input

    There are about 40 cases. Process to the end of file.

    Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Then m integers, G1, G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D<= 30000. Then C different targets. Each target is described by three integers, 0 <= T < m, 0 <= L <= R <= 100.

    Output

    For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

    Output a blank line after each case.

     题目大意:文文要要给幻想乡中的少女拍照。一共有n天,m个少女,在这n天中文文至少要给少女i(i<m)拍Gi张照片。在第t(t<n)中,只能拍Dt张照片,只能给特定的Ct个人拍照,而且每个人至少拍L张,但是少女只给文文拍R张。问文文能否完成任务,在完成任务的情况下最多能拍多少张照片。

    思路:预备知识,无源无汇的上下界可行流,见SGU 194 Reactor Cooling(无源无汇上下界可行流)

    然后来建图:引入源点S汇点T,n天一天一点,m个少女每人一点。S到每一天连一条边,上界为Dt,下界为0。每个少女到汇点连一条边,上界为无穷大,下界为Gi。对每一天,连一条边到所有要拍照的少女,下界为L,上界为R。然后引入超级源点SS,超级汇点TT,像无源无汇上下界可行流那样建边,然后T到S连一条边,容量为无穷大。最后从源点S到汇点T跑一遍最大流就是答案,每条边容量的取法和无源无汇上下界可行流一样。

    小证明:原图为什么是那样建的我就不解释了。

    至于加入超级源点和超级汇点之后为什么要连一条边T→S呢,因为根据无源无汇上下界可行流的做法,原图无源无汇,是应该有一个环的,而现在没有……

    之后再从S到T跑一遍,会不会影响之前的流量下界呢?满足下界是来自于点与超级源点和超级汇点之间的边,如果我们把这两个点删掉,边的容量就没法变了,也就不会影响到流量下界。而我没有删掉,是因为若求出可行解之后,超级源点和超级汇点关联的边都应该是满流量的,再跑最大流不可能经过这两个点。

    那么跑可行流的时候,和跑最大流的时候,流量是分开算的,那为什么不用加上原来可行流的流量呢?因为原先我们连了一条边T→S,根据无源无汇图中,每个点的入流等于出流,跑可行流的流量已经存在了T→S这条边中,跑最大流就时候,T→S的反向边就有了可行流的容量。所以跑最大流的时候会加上可行流的容量,就不用再加一遍了。

    PS:之前以为输出每天的拍照数,后面跑样例的时候发现输出不对……然后把输出改成每天每人,忘了改数组大小RE了一遍>_<

    代码(190MS):

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 using namespace std;
      6 
      7 const int MAXN = 2010;
      8 const int MAXE = 1000010;
      9 const int INF = 0x3fff3fff;
     10 
     11 struct SAP {
     12     int head[MAXN], gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
     13     int to[MAXE], next[MAXE], flow[MAXE], cap[MAXE];
     14     int n, ecnt, st, ed;
     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; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++;
     23         to[ecnt] = u; cap[ecnt] = 0; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
     24         //printf("%d->%d %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(cap[p ^ 1] > 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(cap[p] > flow[p] && dis[u] == dis[v] + 1) {
     58                     flag = true;
     59                     minFlow = min(minFlow, cap[p] - 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(cap[p] > flow[p] && dis[v] < minDis) {
     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 } G;
     90 
     91 int n, m, c, d, l, r, x;
     92 int f[MAXN], down[MAXE], id[MAXE];
     93 
     94 int main() {
     95     while(scanf("%d%d", &n, &m) != EOF) {
     96         G.init();
     97         memset(f, 0, sizeof(f));
     98         int s = n + m + 1, t = s + 1;
     99         int ss = t + 1, tt = ss + 1;
    100         for(int i = 1; i <= m; ++i) {
    101             scanf("%d", &x);
    102             f[i] -= x;
    103             f[t] += x;
    104             G.add_edge(i, t, INF);
    105         }
    106         int cnt = 0;
    107         for(int i = 1; i <= n; ++i) {
    108             scanf("%d%d", &c, &d);
    109             G.add_edge(s, i + m, d);
    110             while(c--) {
    111                 scanf("%d%d%d", &x, &l, &r);
    112                 f[i + m] -= l;
    113                 f[x + 1] += l;
    114                 down[++cnt] = l; id[cnt] = G.ecnt;
    115                 G.add_edge(i + m, x + 1, r - l);
    116             }
    117         }
    118         int sum = 0;
    119         for(int i = 1; i <= t; ++i) {
    120             if(f[i] > 0) sum += f[i], G.add_edge(ss, i, f[i]);
    121             else G.add_edge(i, tt, -f[i]);
    122         }
    123         G.add_edge(t, s, INF);
    124         if(sum != G.Max_flow(ss, tt, tt)) puts("-1");
    125         else {
    126             printf("%d
    ", G.Max_flow(s, t, tt));
    127             for(int i = 1; i <= cnt; ++i) printf("%d
    ", down[i] + G.flow[id[i]]);
    128         }
    129         puts("");
    130     }
    131 }
    View Code
  • 相关阅读:
    How to Create a site at the specified URL and new database (CommandLine Operation)
    Using Wppackager to Package and Deploy Web Parts for Microsoft SharePoint Products and Technologies
    SQL Server Monitor v0.5 [Free tool]
    How to build Web Part
    Deploy web part in a virtual server by developing a Web Part Package file(.cab)
    How to recreate "sites" link if you delete it accidentally
    SharePoint Portal Server管理匿名访问设置
    Monitor sql connection from .Net SqlClient Data Provider
    Brief installation instruction of Sharepoint Portal Server
    How to Use SharePoint Alternate URL Access
  • 原文地址:https://www.cnblogs.com/oyking/p/3252791.html
Copyright © 2011-2022 走看看