POJ 3436 ACM Computer Factory

来源:互联网 发布:淘宝手表男士 编辑:程序博客网 时间:2024/05/17 04:01
ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal 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 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 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
做这个题目我真想吐了,费了九牛二虎之力终于写出来了,结果确是一遍一遍的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;}

原创粉丝点击