ACM Computer Factory--最大流EK()算法

来源:互联网 发布:如何翻墙 知乎 编辑:程序博客网 时间:2024/05/29 11:53

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7026 Accepted: 2486 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 jDi,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 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 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.
题目链接:http://poj.org/problem?id=3436


啊啊啊,这题题意太让人疯了,看了好几个博客,我链个链接,题意写的很清楚;

http://www.cnblogs.com/0803yijia/archive/2013/01/18/2866705.html

哎,上来敲了一个板子,但是并不会建图,源点和汇点找不到TAT,想了半天还是去搜了题解,网上好多说拆分做,并没有看懂,还有好多大神用的各种方法做,都AC了,哎,太渣了,我的方法是找一个超级源点和超级汇点(虚拟的),然后就是建图了,这个详见代码吧,然后就是裸地网络流了。


我还是太渣了,网络流还是不会啊,看来得刷个网络流的专题啊,这个专题太难了,加油吧。哎。


代码:

#include <iostream>#include <cstring>#include <queue>#include <cstdio>#define inf 0x3f3f3f3fusing namespace std;int map1[100][100],pre[1000],vis[1000],n;struct node{    int in[100];    int out[100];    int k;}xin[100];int EK()//模板{    int i,ans=0,now,min1;    queue <int> q;    while(1)//详见我的上一篇博客    {        memset(pre,-1,sizeof(pre));        memset(vis,0,sizeof(vis));        while(!q.empty())        {            q.pop();        }        q.push(0);        vis[0]=1;        while(!q.empty())        {            now=q.front();            q.pop();            if(now==n+1)            {                break;            }            for(i=0;i<=n+1;i++)            {                if(!vis[i]&&map1[now][i]>0)                {                    pre[i]=now;                    vis[i]=1;                    q.push(i);                }            }        }        if(!vis[n+1])        {            break;        }        min1=inf;        for(i=n+1;i!=0;i=pre[i])        {            if(map1[pre[i]][i]<min1)            {                min1=map1[pre[i]][i];            }        }        ans+=min1;        for(i=n+1;i!=0;i=pre[i])        {            map1[pre[i]][i]-=min1;            map1[i][pre[i]]+=min1;        }    }    return ans;}int main(){    int p,b[100][100],flag,an,i,j;    char s[1000];    //while(gets(s)!=NULL)//这个太坑了!!!没有组数   // {我的天哪,强行防AC!!!!注意就好了        while(~scanf("%d%d",&p,&n))        {int cut=0,ji[100][10];        for(i=1;i<=n;i++)        {            scanf("%d",&xin[i].k);            for(j=0;j<p;j++)//输入            {                scanf("%d",&xin[i].in[j]);            }            for(j=0;j<p;j++)            {                scanf("%d",&xin[i].out[j]);            }        }        memset(map1,0,sizeof(map1));//地图        for(i=1;i<=n;i++)        {            int fs=1,ft=1;            for(j=0;j<p;j++)            {                if(xin[i].in[j]==1)//判断是否和源点相连                    fs=0;                if(xin[i].out[j]==0)//判断是否和汇点相连                    ft=0;            }            if(fs)                map1[0][i]=xin[i].k;            if(ft)                map1[i][n+1]=xin[i].k;            for(j=1;j<=n;j++)//判断是否和其他点相连            {                if(i!=j)                {                    flag=1;                    for(int k=0;k<p;k++)                    {                        if(xin[i].out[k]+xin[j].in[k]==1)                        {                            flag=0;                            break;                        }                    }                    if(flag)                        map1[i][j]=min(xin[i].k,xin[j].k);                }            }        }        memcpy(b,map1,sizeof(map1));//备份数组,最后比较用        an=EK();        for(i=1;i<=n;i++)//注意范围,超级源点和超级汇点是我们虚构出来的,其实并不存在        {            for(j=1;j<=n;j++)            {                if(b[i][j]>map1[i][j])                {                    ji[cut][0]=i;                    ji[cut][1]=j;                    ji[cut][2]=b[i][j]-map1[i][j];                    cut++;                }            }        }        //printf("%s\n",s);//没有组数!        printf("%d %d\n",an,cut);//输出        for(i=0;i<cut;i++)        {            printf("%d %d %d\n",ji[i][0],ji[i][1],ji[i][2]);        }    }    return 0;}


这是个大神的博客,然而并没有看懂。。。

http://www.cnblogs.com/pony1993/archive/2012/12/07/2807262.html

0 0