POJ3436-ACM Computer Factory

来源:互联网 发布:java自学还是培训 编辑:程序博客网 时间:2024/06/15 01:38

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8159 Accepted: 2961 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 13 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1Sample input 23 55   0 0 0  0 1 0100 0 1 0  1 0 13   0 1 0  1 1 01   1 0 1  1 1 0300 1 1 2  1 1 1Sample input 32 2100  0 0  1 0200  0 1  1 1

Sample Output

Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 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台机器,每台机器对于自己能加工的半成品电脑有限制,但是处理完的电脑都是一样的,在输入中, 对于第i台机器,第1个数是qi表示这台机器的处理速度,接下来p个数表示这台机器对于能加工的半成品的限制,0表示不能有这个部件,1表示必须有,2表示可有可无。然后又是p个数表示这台机器处理完之后的情况,0表示没有这个部件,1表示有这个部件。求最大的处理速度

解题思路:其实就是一个最大流,对于每台机器的处理速度,将其拆点为入口和出口,在入口和出口之间连接一条容量为处理速度的边。 建立超级源汇点S,T,对于每台机器,如果Input中没有1,就从S连一条容量为INF的边到这台机器的入口;如果Output中都为1,则从这台机器的出口连接一条容量为INF的边到T。 对于两个机器i,j,若i的Output与j的Input相符,就从i的出口连接一条容量为INF的边到j的入口。最后在跑一边一台机器出口到另一台机器入口流量的变化即可



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 500struct node{    int u, v, next, cap;} edge[MAXN*MAXN];int nt[MAXN], s[MAXN], d[MAXN],a[MAXN][MAXN];int cnt,ans[MAXN*MAXN][3];void init(){    cnt = 0;    memset(s, -1, sizeof(s));}void add(int u, int v, int c){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].cap = c;    edge[cnt].next = s[u];    s[u] = cnt++;    edge[cnt].u = v;    edge[cnt].v = u;    edge[cnt].cap = 0;    edge[cnt].next = s[v];    s[v] = cnt++;}bool BFS(int ss, int ee){    memset(d, 0, sizeof d);    d[ss] = 1;    queue<int>q;    q.push(ss);    while (!q.empty())    {        int pre = q.front();        q.pop();        for (int i = s[pre]; ~i; i = edge[i].next)        {            int v = edge[i].v;            if (edge[i].cap > 0 && !d[v])            {                d[v] = d[pre] + 1;                q.push(v);            }        }    }    return d[ee];}int DFS(int x, int exp, int ee){    if (x == ee||!exp) return exp;    int temp,flow=0;    for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)    {        int v = edge[i].v;        if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)        {            edge[i].cap -= temp;            edge[i ^ 1].cap += temp;            flow += temp;            exp -= temp;            if (!exp) break;        }    }    if (!flow) d[x] = 0;    return flow;}int Dinic_flow(int ss, int ee){    int ans = 0;    while (BFS(ss, ee))    {        for (int i = 0; i <= ee; i++) nt[i] = s[i];        ans+= DFS(ss, INF, ee);    }    return ans;}int main(){    int n, p;    while (~scanf("%d %d", &p, &n))    {        init();        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i][0]);            add(i,i+n,a[i][0]);            for(int j=1;j<=2*p;j++)                scanf("%d",&a[i][j]);        }        int ss=0,ee=n*2+1;        for(int i=1;i<=n;i++)        {            int flag1=1,flag2=1;            for(int j=1;j<=p;j++)            {                if(a[i][j]==1) flag1=0;                if(!a[i][j+p]) flag2=0;            }            if(flag1) add(ss,i,INF);            if(flag2) add(i+n,ee,INF);        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if(i==j) continue;                int flag=1;                for(int k=1;k<=p;k++)                    if((a[i][k+p]&&!a[j][k])||(!a[i][k+p]&&a[j][k]==1)) {flag=0;break;}                if(flag) add(i+n,j,INF);            }        }        printf("%d ",Dinic_flow(ss,ee));        int res=0;        for(int i=n+1;i<=2*n;i++)        {            for(int j=s[i];~j;j=edge[j].next)            {                if(edge[j].v==ee||edge[j].v==i-n) continue;                if(edge[j].cap!=INF)                {                    ans[res][0]=edge[j].u-n;                    ans[res][1]=edge[j].v;                    ans[res++][2]=INF-edge[j].cap;                }            }        }        printf("%d\n",res);        for(int i=0;i<res;i++)            printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);    }    return 0;}

原创粉丝点击