zoukankan      html  css  js  c++  java
  • ACM Computer Factory(dinic)

    ACM Computer Factory
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5596   Accepted: 1922   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
    跟poj1459差不多,就是net要自己连上线
    dinic 0ms 
      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<queue>
      4 #include<string.h>
      5 using namespace std;
      6 const int M = 60 , inf = 0x3f3f3f3f ;
      7 struct edge
      8 {
      9     int u , v , timeu ;
     10     int w ;
     11 }e[M * M * 2];
     12 
     13 struct node
     14 {
     15     int input[20] , output[20] ;
     16     int w ;
     17 }o[M];
     18 
     19 int p , n ;
     20 int src , des ;
     21 int dis[M] ;
     22 int head[M * M * 2] ;
     23 int cnt , app ;
     24 struct ABW
     25 {
     26     int a , b , w ;
     27 }step[M * M * 2];
     28 
     29 bool bfs ()
     30 {
     31     queue <int> q ;
     32     while (!q.empty ())
     33         q.pop () ;
     34     memset (dis , -1 , sizeof(dis)) ;
     35     dis[src] = 0 ;
     36     q.push (src) ;
     37     while (!q.empty ()) {
     38         int u = q.front () ;
     39         q.pop () ;
     40         for (int i = head[u] ; i != -1 ; i = e[i].timeu) {
     41             int v = e[i].v ;
     42             if (dis[v] == -1 && e[i].w > 0) {
     43                 dis[v] = dis[u] + 1 ;
     44                 q.push (v) ;
     45             }
     46         }
     47     }
     48     if (dis[des] > 0)
     49         return true ;
     50     return false ;
     51 }
     52 
     53 int dfs (int u , int low)
     54 {
     55     int a = 0 ;
     56     if (u == des)
     57         return low ;
     58     for (int i = head[u] ; i != -1 ; i = e[i].timeu) {
     59         int v = e[i].v ;
     60         if (e[i].w > 0 && dis[v] == dis[u] + 1 && (a = dfs (v , min (low , e[i].w)))) {
     61             e[i].w -= a ;
     62             if (e[i].u != src && e[i].v != des) {
     63                 step[app].a = e[i].u ; step[app].b = e[i].v ; step[app].w = a ;
     64                 app++ ;
     65             }
     66             e[i^1].w += a ;
     67             return a ;
     68         }
     69     }
     70     dis[u] = -1 ;
     71     return 0 ;
     72 }
     73 
     74 void dinic ()
     75 {
     76     int ans = 0 , res = 0 ;
     77     app = 0 ;
     78     while (bfs ()) {
     79         while (1) {
     80             if (ans = dfs (src , inf))
     81                 res += ans ;
     82             else
     83                 break ;
     84         }
     85     }
     86     printf ("%d %d
    " , res , app) ;
     87     for (int i = app - 1 ; i >= 0 ; i--)
     88         printf ("%d %d %d
    " , step[i].a + 1 , step[i].b + 1 , step[i].w) ;
     89 }
     90 
     91 void addedge (int u , int v)
     92 {
     93     e[cnt].u = u ; e[cnt].v = v ; e[cnt].w = o[u].w == -1 ? o[v].w : o[u].w ; e[cnt].timeu = head[u] ;
     94     head[u] = cnt++ ;
     95     e[cnt].u = v ; e[cnt].v = u ; e[cnt].w = 0 ; e[cnt].timeu = head[v] ;
     96     head[v] = cnt++ ;
     97 }
     98 
     99 void binary (int s , int l , int r)
    100 {
    101     if (l == r) {
    102         if (s != l && l != n) {
    103             int i ;
    104             for (i = 0 ; i < p ; i++) {
    105                 if (o[l].input[i] != 2 && o[s].output[i] != o[l].input[i])
    106                     break ;
    107             }
    108             if (i == p) {
    109                 addedge (s , l) ;
    110             }
    111         }
    112         return ;
    113     }
    114     int mid = l + r >> 1 ;
    115     binary (s , l , mid) ;
    116     binary (s , mid + 1 , r) ;
    117 }
    118 
    119 
    120 
    121 int main ()
    122 {
    123    // freopen ("a.txt" , "r" , stdin) ;
    124     while (~ scanf ("%d%d" , &p , &n)) {
    125         for (int i = 0 ; i < n ; i++) {
    126             scanf ("%d" , &o[i].w) ;
    127             for (int j = 0 ; j < p ; j++) {
    128                 scanf ("%d" , &o[i].input[j]) ;
    129             }
    130             for (int j = 0 ; j < p ; j++) {
    131                 scanf ("%d" , &o[i].output[j]) ;
    132             }
    133         }
    134         for (int i = 0 ; i < p ; i++) {
    135             o[n].input[i] = o[n].output[i] = 0 ;//源点
    136             o[n + 1].input[i] = o[n + 1].output[i] = 1 ;//汇点
    137         }
    138         o[n].w = -1 , o[n + 1].w = -1 ;
    139         src = n , des = n + 1 ;
    140 
    141         n += 2 ;
    142         cnt = 0 ;
    143         memset (head , -1 , sizeof(head)) ;
    144         for (int i = 0 ; i < n - 1; i++) {
    145             binary(i , 0 , n) ;
    146         }
    147        /* for (int i = 0 ; i < cnt ; i++) {
    148             if (i % 2 == 0)
    149             printf ("%d-->%d === %d , time: %d
    " , e[i].u , e[i].v , e[i].w , e[i].timeu) ;
    150         }
    151         puts ("") ; */
    152         dinic () ;
    153     }
    154     return 0 ;
    155 }
    View Code
  • 相关阅读:
    在Qt中使用sleep
    Qt深入:不能不知道的Type、Attribute和Flags
    浅析mysql 共享表空间与独享表空间以及他们之间的转化
    taobao月报 ---mysql汇总
    slave_net_timeout
    LINUX 内核算杂 七杂 八
    Hadoop可视化与交互式工具:Zeppelin和Hue
    看开源代码利器—用Graphviz + CodeViz生成C/C++函数调用图(call graph)
    python 学习笔记十四 jQuery案例详解(进阶篇)
    MySQL如何选择float, double, decimal
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4319988.html
Copyright © 2011-2022 走看看