ACM Computer Factory POJ - 3436

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total 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 2 P + 1 integers Qi Si,1Si,2...Si,P Di,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 machines A and B must be described by three positive numbers A B W, whereW 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

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


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

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

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
用样例分析:机器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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值