• POJ3436 ACM Computer Factory —— 最大流


    题目链接:https://vjudge.net/problem/POJ-3436

    ACM Computer Factory
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8544   Accepted: 3102   Special Judge

    Description

    As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

    Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

    Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

    Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.

    Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.

    The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

    After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

    As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

    Input

    Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2...Si,P Di,1 Di,2...Di,P, where Qi specifies performance, Si,j— input specification for part jDi,k — output specification for part k.

    Constraints

    1 ≤ P ≤ 10, 1 ≤ ≤ 50, 1 ≤ Qi ≤ 10000

    Output

    Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.

    If several solutions exist, output any of them.

    Sample Input

    Sample input 1
    3 4
    15  0 0 0  0 1 0
    10  0 0 0  0 1 1
    30  0 1 2  1 1 1
    3   0 2 1  1 1 1
    Sample input 2
    3 5
    5   0 0 0  0 1 0
    100 0 1 0  1 0 1
    3   0 1 0  1 1 0
    1   1 0 1  1 1 0
    300 1 1 2  1 1 1
    Sample input 3
    2 2
    100  0 0  1 0
    200  0 1  1 1

    Sample Output

    Sample output 1
    25 2
    1 3 15
    2 3 10
    Sample output 2
    4 5
    1 3 3
    3 5 3
    1 2 1
    2 4 1
    4 5 1
    Sample output 3
    0 0

    Hint

    Bold texts appearing in the sample sections are informative and do not form part of the actual data.

    Source

    Northeastern Europe 2005, Far-Eastern Subregion

    题意:

    在一个电脑厂中,一台电脑被分成P个部件,有N台机器,且每台都有其特定的输入部件和输出部件。其中对于输入部件:0代表不能有,1代表必须有,2代表可以可无。对于输出部件:0代表没有,1代表有。因此每台机器都可能存在合作关系:即如果机器A的输出满足机械B的输入,就可以把机器A的成品放到机械B中继续加工。问:怎样安排流水线,才能使得单位时间内制造的电脑最多?

    题解:

    不拆点做法:

    1.建立超级源点,且超级源点与每个输入都为0的机器相连,边的容量为这台机器的容量,表明最多只能提供机器所能容纳的量。

    2.如果机器A的输出满足机械B的输入,则把机械A与机械B相连,且边的容量为机械A和机械B的容量的最小值,表明机械A最多只能为机械B提供自己所拥有的全部并且机械B也能接受的。

    3.建立超级汇点,且每个输出都为1的机器与超级汇点相连,边的容量为这台机器的容量,表明这台机器最多只能产出自己容量大小的电脑。

    4.求最大流即可。

    拆点的做法:

    1.只是对“不拆点做法”稍加修改:原本连向超级源点或者汇点的边的容量改为无限大,然后对每台机器拆成两个点,内部连一条边,边的容量为这台机器的容量。

    2.对机器进行拆点只不过是为了:使得流经此台机器的流量限制在机器容量的范围内。那为什么“不拆点做法”又可以不拆点呢?因为在与超级源点、超级汇点相连的时候,已经把流经每台机器的流量限制住了。

    不拆点:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int mod = 1e9+7;
     17 const int MAXN = 1e2+10;
     18 
     19 int maze[MAXN][MAXN];
     20 int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
     21 int flow[MAXN][MAXN];
     22 
     23 int sap(int start, int end, int nodenum)
     24 {
     25     memset(cur, 0, sizeof(cur));
     26     memset(dis, 0, sizeof(dis));
     27     memset(gap, 0, sizeof(gap));
     28     memset(flow, 0, sizeof(flow));
     29     int u = pre[start] = start, maxflow = 0, aug = INF;
     30     gap[0] = nodenum;
     31 
     32     while(dis[start]<nodenum)
     33     {
     34         loop:
     35         for(int v = cur[u]; v<nodenum; v++)
     36         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
     37         {
     38             aug = min(aug, maze[u][v]-flow[u][v]);
     39             pre[v] = u;
     40             u = cur[u] = v;
     41             if(v==end)
     42             {
     43                 maxflow += aug;
     44                 for(u = pre[u]; v!=start; v = u, u = pre[u])
     45                 {
     46                     flow[u][v] += aug;
     47                     flow[v][u] -= aug;
     48                 }
     49                 aug = INF;
     50             }
     51             goto loop;
     52         }
     53 
     54         int mindis = nodenum-1;
     55         for(int v = 0; v<nodenum; v++)
     56             if(maze[u][v]-flow[u][v]>0 && mindis>dis[v])
     57             {
     58                 cur[u] = v;
     59                 mindis = dis[v];
     60             }
     61         if((--gap[dis[u]])==0) break;
     62         gap[dis[u]=mindis+1]++;
     63         u = pre[u];
     64     }
     65     return maxflow;
     66 }
     67 
     68 int cap[MAXN], in[MAXN][20], out[MAXN][20];
     69 int cnt, ans[MAXN*MAXN][3];
     70 int main()
     71 {
     72     int n, p;
     73     while(scanf("%d%d",&p,&n)!=EOF)
     74     {
     75         for(int i = 1; i<=n; i++)
     76         {
     77             scanf("%d", &cap[i]);
     78             for(int j = 1; j<=p; j++) scanf("%d", &in[i][j]);
     79             for(int j = 1; j<=p; j++) scanf("%d", &out[i][j]);
     80         }
     81 
     82         int start = 0, end = n+1;
     83         memset(maze, 0, sizeof(maze));
     84         for(int i = 1; i<=n; i++)
     85         {
     86             for(int j = 1; j<=n; j++)
     87             {
     88                 if(i==j) continue;
     89                 bool flag = true;
     90                 for(int k = 1; k<=p; k++)
     91                     if(out[i][k]+in[j][k]==1)
     92                         flag = false;
     93 
     94                 if(flag) maze[i][j] = min(cap[i], cap[j]);
     95             }
     96 
     97             bool flag1 = true, flag2 = true;
     98             for(int k = 1; k<=p; k++)
     99             {
    100                 if(in[i][k]==1) flag1 = false;
    101                 if(out[i][k]==0) flag2 = false;
    102             }
    103             if(flag1) maze[start][i] = cap[i];
    104             if(flag2) maze[i][end] = cap[i];
    105         }
    106 
    107         int maxflow = sap(start, end, n+2);
    108         cnt = 0;
    109         for(int i = 1; i<=n; i++)
    110         for(int j = 1; j<=n; j++)
    111         {
    112             if(i==j) continue;
    113             if(flow[i][j]>0)
    114             {
    115                 ans[++cnt][0] = i;
    116                 ans[cnt][1] = j;
    117                 ans[cnt][2] = flow[i][j];
    118             }
    119         }
    120 
    121         printf("%d %d
    ", maxflow, cnt);
    122         for(int i = 1; i<=cnt; i++)
    123             printf("%d %d %d
    ", ans[i][0], ans[i][1], ans[i][2]);
    124     }
    125 }
    View Code

    拆点:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int mod = 1e9+7;
     17 const int MAXN = 1e2+10;
     18 
     19 int maze[MAXN][MAXN];
     20 int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
     21 int flow[MAXN][MAXN];
     22 
     23 int sap(int start, int end, int nodenum)
     24 {
     25     memset(cur, 0, sizeof(cur));
     26     memset(dis, 0, sizeof(dis));
     27     memset(gap, 0, sizeof(gap));
     28     memset(flow, 0, sizeof(flow));
     29     int u = pre[start] = start, maxflow = 0, aug = INF;
     30     gap[0] = nodenum;
     31 
     32     while(dis[start]<nodenum)
     33     {
     34         loop:
     35         for(int v = cur[u]; v<nodenum; v++)
     36         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
     37         {
     38             aug = min(aug, maze[u][v]-flow[u][v]);
     39             pre[v] = u;
     40             u = cur[u] = v;
     41             if(v==end)
     42             {
     43                 maxflow += aug;
     44                 for(u = pre[u]; v!=start; v = u, u = pre[u])
     45                 {
     46                     flow[u][v] += aug;
     47                     flow[v][u] -= aug;
     48                 }
     49                 aug = INF;
     50             }
     51             goto loop;
     52         }
     53 
     54         int mindis = nodenum-1;
     55         for(int v = 0; v<nodenum; v++)
     56             if(maze[u][v]-flow[u][v]>0 && mindis>dis[v])
     57             {
     58                 cur[u] = v;
     59                 mindis = dis[v];
     60             }
     61         if((--gap[dis[u]])==0) break;
     62         gap[dis[u]=mindis+1]++;
     63         u = pre[u];
     64     }
     65     return maxflow;
     66 }
     67 
     68 int cap[MAXN], in[MAXN][20], out[MAXN][20];
     69 int cnt, ans[MAXN*MAXN][3];
     70 int main()
     71 {
     72     int n, p;
     73     while(scanf("%d%d",&p,&n)!=EOF)
     74     {
     75         for(int i = 1; i<=n; i++)
     76         {
     77             scanf("%d", &cap[i]);
     78             for(int j = 1; j<=p; j++) scanf("%d", &in[i][j]);
     79             for(int j = 1; j<=p; j++) scanf("%d", &out[i][j]);
     80         }
     81 
     82         int start = 0, end = 2*n+1;
     83         memset(maze, 0, sizeof(maze));
     84         for(int i = 1; i<=n; i++)
     85         {
     86             maze[i][n+i] = cap[i];
     87             for(int j = 1; j<=n; j++)
     88             {
     89                 if(i==j) continue;
     90                 bool flag = true;
     91                 for(int k = 1; k<=p; k++)
     92                     if(out[i][k]+in[j][k]==1)
     93                         flag = false;
     94 
     95                 if(flag) maze[n+i][j] = INF;
     96             }
     97 
     98             bool flag1 = true, flag2 = true;
     99             for(int k = 1; k<=p; k++)
    100             {
    101                 if(in[i][k]==1) flag1 = false;
    102                 if(out[i][k]==0) flag2 = false;
    103             }
    104             if(flag1) maze[start][i] = INF;
    105             if(flag2) maze[n+i][end] = INF;
    106         }
    107 
    108         int maxflow = sap(start, end, 2*n+2);
    109         cnt = 0;
    110         for(int i = 1; i<=n; i++)
    111         for(int j = 1; j<=n; j++)
    112         {
    113             if(i==j) continue;
    114             if(flow[n+i][j])
    115             {
    116                 ans[++cnt][0] = i;
    117                 ans[cnt][1] = j;
    118                 ans[cnt][2] = flow[n+i][j];
    119             }
    120         }
    121 
    122         printf("%d %d
    ", maxflow, cnt);
    123         for(int i = 1; i<=cnt; i++)
    124             printf("%d %d %d
    ", ans[i][0], ans[i][1], ans[i][2]);
    125     }
    126 }
    View Code
  • 相关阅读:
    设计模式:访问者模式
    设计模式:模板模式
    三分法——求解凸性函数的极值问题——czyuan原创
    素数&&Miller_Rabbin
    【算法入门】深度优先搜索(DFS)
    快速幂取模
    hrbeu1280Turn the corner
    hdoj_3400Line belt
    【专题】三分法和牛顿迭代法总结
    zoj_3203Light Bulb
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8066382.html
Copyright © 2020-2023  润新知