# POJ 3436 ACM Computer Factory （网络流，最大流）

### 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 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 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

## 代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int maxN=200;
const int maxP=30;
const int inf=2147483647;

class Edge//输出时要用
{
public:
int u,v,w;
};

int n,m,P;
int G[maxN][maxN];//图
int G2[maxN][maxN];//备份的图，用来对比输出
int W[maxN];//每个机器人的效率
int In[maxN][maxP];//机器人对入口的要求
int Out[maxN][maxP];//机器人的出口规格
int Path[maxN];//EK算法中存增广路径
int Flow[maxN];//流
Edge Outp[maxN*maxN];//用于输出

bool bfs();//bfs增广

int main()
{
memset(G,0,sizeof(G));
scanf("%d%d",&P,&n);
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]);
}
for (int i=1;i<=n;i++)//给每个点的入点到出点连一条边
G[i][i+n]=W[i];
for (int i=1;i<=n;i++)//给每对出口与入口相对应的点连一条边
for (int j=1;j<=n;j++)
{
bool ok=1;
for (int k=1;k<=P;k++)
if (((Out[i][k]==1)&&(In[j][k]==0))||((Out[i][k]==0)&&(In[j][k]==1)))
{
ok=0;
break;
}
if (ok)
G[i+n][j]=min(W[i],W[j]);
}
for (int i=1;i<=n;i++)//判断能否和汇点或源点连接
{
bool ok=1;
for (int j=1;j<=P;j++)
if (In[i][j]==1)
{
ok=0;
break;
}
if (ok)
G[0][i]=inf;
ok=1;
for (int j=1;j<=P;j++)
if (Out[i][j]==0)
{
ok=0;
break;
}
if (ok)
G[i+n][n*2+1]=inf;
}
memcpy(G2,G,sizeof(G2));//把G存起来，备份一边，方便后面输出答案
int Ans=0;
while (bfs())//笔者这里用EK算法实现最大流
{
int di=Flow[n*2+1];
int now=2*n+1;
int last=Path[now];
while (now!=0)//更新残量网络
{
G[last][now]-=di;
G[now][last]+=di;
now=last;
last=Path[now];
}
Ans+=di;
}
printf("%d ",Ans);//输出答案，统计路径
int top=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (G2[i+n][j]>G[i+n][j])
{
top++;
Outp[top].u=i;
Outp[top].v=j;
Outp[top].w=G2[i+n][j]-G[i+n][j];
}
printf("%d\n",top);//输出路径
for (int i=1;i<=top;i++)
printf("%d %d %d\n",Outp[i].u,Outp[i].v,Outp[i].w);
return 0;
}

bool bfs()
{
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);
Flow[0]=inf;
do
{
int u=Q.front();
Q.pop();
for (int i=0;i<=2*n+1;i++)
{
if ((Path[i]==-1)&&(G[u][i]>0))
{
Path[i]=u;
Q.push(i);
Flow[i]=min(Flow[u],G[u][i]);
}
}
}
while (!Q.empty());
if (Flow[2*n+1]>0)
return 1;
return 0;
}
posted @ 2017-07-29 14:00  SYCstudio  阅读(...)  评论(...编辑  收藏