ACM Computer Factory POJ - 3436

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 j, Di,k — output specification for part k.

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, 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.

题目大致意思:

组装一个计算机需要p个零件同时存在。现在给你n个机器,每个机器都用特定的输入输出格式。如果计算机半成品满足输入格式就可以进入这个机器,同事以输出格式输出。

现在给你这n个计算机的生产效率,输入输出格式,让你求出如何组织这些计算机可以让工厂的生产效率最高,并且输出组装方案。

思路:

枚举所有合法的情况,建立边,容量为机器效率,把无输入限制(输入格式没有1)都链接到超级起点,把无输出限制的点都链接到超级终点,然后从超级起点到超级终点跑一边程序,记录下路径就可以了。网上虽然大部分都是分点什么的,但是这个题不用(想了一下应该是不需要的)。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=60;
int save1[60][11];
int save2[60][11];
int saves[60];
int mem[maxn][maxn];
int t;
struct node
{
    int u,v,next,c;
};
node edge[100000];
int head[maxn];
int cnt;
int dis[maxn];
int n,m;
int ans;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    memset(dis,-1,sizeof(dis));
    ans=0;
}
void add(int a,int b,int c)
{
    edge[cnt].u=a;
    edge[cnt].v=b;
    edge[cnt].c=c;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
int bfs()// 给各点分层,离源点的远近分
{
    memset(dis, -1, sizeof(dis));
    queue<int> q;
    dis[0] = 0;
    q.push(0);
    int i;
    int cur;
    while(!q.empty())
    {
        cur = q.front();
        q.pop();
        for(i = head[cur]; i != -1; i = edge[i].next)
        {
            if(dis[edge[i].v] == -1 && edge[i].c > 0)
            {
                dis[edge[i].v] = dis[cur] + 1;
                q.push(edge[i].v);
            }
        }
    }
    if(dis[m+1] < 0)
        return 0;
    return 1;
}
int Find(int x,int low) //找增广
{
    int a;
    if(x==m+1) return low;
    for(int i=head[x]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(dis[v]==dis[x]+1 && edge[i].c>0 &&(a=Find(v,min(low,edge[i].c))))
        {
            edge[i].c -=a;
            edge[i^1].c +=a;
            if(v!=m+1&&x)
            {
           // printf("***%d %d**\n",x,v);
                if(!mem[x][v])
                    t++;
                mem[x][v]+=a;
            }
            return a;
        }
    }
    return 0;
}
void dinic()
{
    int temp;
    memset(mem,0,sizeof(mem));
    t=0;
    while(bfs())
    {
        temp=Find(0,0x3f3f3f3f);
        ans+=temp;
    }
    printf("%d %d\n",ans,t);
    for(int i=0; i<maxn; i++)
    {
        for(int j=0; j<maxn; j++)
        {
            if(mem[i][j])
                printf("%d %d %d\n",i,j,mem[i][j]);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&saves[i]);
            for(int j=1; j<=n; j++)
                scanf("%d",&save1[i][j]);
            for(int j=1; j<=n; j++)
                scanf("%d",&save2[i][j]);
        }
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int flag=1;
                if(i!=j)
                {
                    for(int s=1; s<=n; s++)
                    {
                        if(save1[j][s]!=2&&save2[i][s]!=save1[j][s])
                        {
                            flag=0;
                        }
                    }
                }
                else
                    flag=0;
                if(flag)
                {
                    add(i,j,saves[i]);
                    add(j,i,0);
                }
            }
        }
        for(int i=1; i<=m; i++)
        {
            int flag=1;
            for(int j=1; j<=n; j++)
            {
                if(save1[i][j]==1)
                    flag=0;
            }
            if(flag)
            {
                add(0,i,saves[i]);
                add(i,0,0);
            }
            flag=1;
            for(int j=1; j<=n; j++)
            {
                if(save2[i][j]!=1)
                    flag=0;
            }
            if(flag)
            {
                add(i,m+1,saves[i]);
                add(m+1,i,0);
            }

        }
        dinic();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值