POJ 3436 ACM Computer Factory

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4328 Accepted: 1449 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 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.

Source

Northeastern Europe 2005, Far-Eastern Subregion
做这个题目我真想吐了,费了九牛二虎之力终于写出来了,结果确是一遍一遍的Wa, 一开始拆点建图,建错图了,改了之后还是Wa, 然后一遍一遍的检查,没有怀疑求路径的时候代码,感觉那是非常正确是,在Wa之下,求路径换了种写发过了,然后就检查原来求路径的代码为什么错了,检查了好长时间,才知道什么地方错了: 原来我是在用EK算法的时候求出路径,这路径并不是真实的路径,从反向边加最小值就可以看出,如果这条边原来不可用,有可能在加值之后就可以用了,这种情况下求出的路径显然是错误的,因为那只是个求最大流的中间过程,如果在中间过程用了那条边,则求出的路径中就会有这条边,而这条边在原始图中是不可用的,那么错误性就是显然的了。 在完整的求出最大流后,然后用每条边剩余的流量和原始流量比,得出的结果才是正确的结果
#include <iostream>
#include <string.h>
using namespace std;
int a[60][60],b[60][60],val[60];
int pre[120],c[120][120];
int res[100][100],queue[100000],Res[100][100];
int TOP;
int sta,end,p,n;
int INF=0x7ffffff;
int main()
{
    int EK();
    int i,j,m,s,t,k,u,resres,v;
    while(cin>>p>>n)
     {
        memset(c,0,sizeof(c));
        memset(Res,0,sizeof(Res));
        sta=0; end=2*n+1;
        for(i=1;i<=n;i++)
        {
            cin>>val[i];
            c[i][n+i]=val[i];
            for(j=1;j<=p;j++)
            {
                cin>>a[i][j];
            }
            for(j=1;j<=p;j++)
            {
                cin>>b[i][j];
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=p;j++)
            {
                if(a[i][j]==1)
                {
                    break;
                }
            }
            if(j==p+1)
            {
                c[0][i]=INF;
            }
            for(j=1;j<=p;j++)
            {
                if(b[i][j]!=1)
                {
                    break;
                }
            }
            if(j==p+1)
            {
                c[n+i][end]=INF;
            }
            for(j=1;j<=n;j++)
            {
                if(i!=j)
                {
                    for(u=1;u<=p;u++)
                    {
                        if(a[j][u]==2||(a[j][u]==b[i][u]))
                        {
                            ;
                        }else
                        {
                            break;
                        }
                    }
                    if(u==p+1)
                    {
                        c[n+i][j]=INF;
                        Res[n+i][j]=INF;
                    }
                }
            }
        }
        TOP=0;t=0;
        t=EK();
        resres=0;
        for(i=n+1;i<=2*n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(Res[i][j]&&c[i][j]<Res[i][j])
                {
                    resres++;
                }
            }
        }
        cout<<t<<" "<<resres<<endl;
        for(i=n+1;i<=2*n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(Res[i][j]&&c[i][j]<Res[i][j])
                {
                    cout<<i-n<<" "<<j<<" "<<Res[i][j]-c[i][j]<<endl;
                }
            }
        }
     }
    return 0;
}
int bfs()
{
    int i,j,base,top,x;
    int min1[100000];
    base=top=0;
    queue[top++]=sta;
    memset(pre,-1,sizeof(pre));
    pre[sta]=-2;
    min1[sta]=INF;
    while(base<top)
    {
        x=queue[base++];
        for(i=1;i<=end;i++)
        {
            if(pre[i]==-1&&c[x][i])
            {
                min1[i]=min1[x]<c[x][i]? min1[x]:c[x][i];
                pre[i]=x;
                queue[top++]=i;
                if(i==end)
                {
                    break;
                }
            }
        }
        if(i!=end+1)
        {
            break;
        }
    }
    if(pre[end]==-1)
    {
        return -1;
    }
    return min1[end];
}
int EK()
{
    int i,j,k,sum=0,t,pre_x,v;
    while(1)
    {
        k=bfs();
        if(k==-1)
        {
            break;
        }
        sum+=k;
        for(t=end,j=0;t!=sta;t=pre[t])
        {
            pre_x=pre[t];
            c[pre_x][t]-=k;
            c[t][pre_x]+=k;
        }
    }
    return sum;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值