ACM Computer Factory POJ

来源:互联网 发布:美工废物利用教案 编辑:程序博客网 时间:2024/06/07 23:56

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8464 Accepted: 3080 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 ofP 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 ofith machine is represented as by 2P + 1 integers Qi Si,1Si,2...Si,PDi,1Di,2...Di,P, whereQi specifies performance,Si,j — input specification for partj,Di,k — output specification for partk.

Constraints

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

Output

Output the maximum possible overall performance, then M — number of connections that must be made, thenM descriptions of the connections. Each connection between machinesA and B must be described by three positive numbers A B W, whereW is the number of computers delivered fromA 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

[Submit]   [Go Back]   [Status]   [Discuss]


题意:给出N台机器,流水线作业装配电脑,每台电脑有P种配件,每台机器有特定的输入输出:

0表示电脑没有此配件,1表示有此配件,机器输入参数中的2表示输入的电脑配件可有可无

3 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1
用样例分析:机器1可把状态(0,0,0)转换成(0,1,0),再由机器3转换成(1,1,1)即完成装配,单位时间装配了15台


分析:

拆点:一台机器拆分出输入和输出两个点

容量:从超级源点s到起点(状态全0),从终点(状态全1)到超级终点t,不同机器之间,容量均为INF,输入和输出之间容量设为单位时间操作次数;


Ac code:

#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>using namespace std;const int INF =0x3f3f3f3f;const int N=1000+50;int w[N];int in[N][N];int out[N][N];int n,p;struct Edge {    int from,to,cap,flow;};vector<Edge> edge;vector<int> G[N];int pre[N];//记录路径int vis[N];//起点到i的可改进量bool bfs(int s,int t) {    memset(vis,0,sizeof(vis));    queue<int>q;    q.push(s);    vis[s]=INF;    while(!q.empty()) {        int x=q.front();        q.pop();        for(int i=0; i<G[x].size(); i++) {            Edge& e=edge[G[x][i]];            if(!vis[e.to]&&e.cap>e.flow) {                vis[e.to]=min(vis[x],e.cap-e.flow);                pre[e.to]=G[x][i];                q.push(e.to);            }        }    }    return vis[t];}int max_flow(int s,int t) {    int flow=0;    while(bfs(s,t)) {        for(int u=t; u!=s; u=edge[pre[u]].from) {            edge[pre[u]].flow+=vis[t];            edge[pre[u]^1].flow-=vis[t];        }        flow+=vis[t];    }    return flow;}void add_edge(int from,int to,int cap) {    edge.push_back((Edge) {from,to,cap,0});    edge.push_back((Edge) {to,from,0,0});    int m=edge.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}int isStart(int x[]) {    for(int i=1; i<=p; i++) {        if(x[i]==1) return 0;    }    return 1;}int isEnd(int x[]) {    for(int i=1; i<=p; i++) {        if(x[i]==0) return 0;    }    return 1;}int isInAndOut(int in[],int out[]) {    for(int i=1; i<=p; i++) {        if(in[i]!=out[i]&&in[i]!=2) return 0;   //等于2可以任意    }    return 1;}void init(int n) {    for(int i=0; i<=n; i++) {        G[i].clear();    }    memset(in,0,sizeof in);    memset(out,0,sizeof out);    //memset(pre,0,sizeof pre);}int main() {    while(~scanf("%d%d",&p,&n)) {        int s=0;        int t=2*n+1;        init(t);        for(int i=1; i<=n; i++) {            scanf("%d",&w[i]);            //拆点,入点和出点            for(int j=1; j<=p; j++)                scanf("%d",&in[i][j]);            for(int j=1; j<=p; j++)                scanf("%d",&out[i][j]);            if(isStart(in[i])) add_edge(s,i,INF);       //1~n为入点            if(isEnd(out[i])) add_edge(n+i,t,INF);      //n+1~2n为出点        }        for(int i=1; i<=n; i++) {            add_edge(i,i+n,w[i]);            for(int j=1; j<=n; j++) {                if(i==j) continue;                if(isInAndOut(in[i],out[j])) {                    add_edge(j+n,i,INF);                //刚好可以连接                }            }        }        int ans=max_flow(s,t);        int cnt=0;        int path[N][3];        for(int u=n+1; u<t; u++)for(int i=0; i<G[u].size(); i++) {            Edge& e=edge[G[u][i]];            if(e.to>0&&e.to<=n&&e.flow>0){                path[cnt][0]=u-n;                path[cnt][1]=e.to;                path[cnt++][2]=e.flow;            }        }        printf("%d %d\n",ans,cnt );        for(int i=0;i<cnt;i++){            printf("%d %d %d\n",path[i][0],path[i][1],path[i][2] );        }    }    return 0;}






原创粉丝点击