您的位置:首页 > 理论基础 > 计算机网络

【POJ 3436 ACM Computer Factory】网络流 & 拆点 & Dinic

2017-10-01 17:06 513 查看
ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 8323 Accepted: 3027 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.

题意 : 给出一些机器,每小时的产量,每台机器需要对应的部件才可以工作 (1 代表必须有 0 代表必须没有 2 代表可有可无),每台机器可以生产出对应的部件(1 代表可以生产出对应的部件,0 代表不可以),能生产出所有部件的机器也意味着可以产出一台完整的机器,问每小时的最多可以生产出多少台完整的1机器

思路: 网络流 & 拆点 ,每台机器拆分成接收部件的机器和产出部件的机器,他们之间的流为该台机器的产量

源点s -> 接收部件的机器 -> 产出部件的机器 -> 汇点

产出部件和接收部件相匹配的机器之间也需要建边,流量无穷大

AC代码:

#include<cstdio>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2e5 + 10;
const int INF = 1e9 + 7;
typedef long long LL;
int head[MAX],d[MAX],nl,p[60][60],w[60][60],sum[60][3],a[60];
void init(){
nl = 0;
memset(head,-1,sizeof head);
}
int read(){
int x = 0, y = 1;
char s = getchar();
while(s < '0' || s > '9') { if(s == '-') y = -1;s = getchar();}
while(s >= '0' && s <= '9') x = x * 10 + s - '0',s = getchar();
return x * y;
}
struct node{
int from,to,cap,flow,next;
}st[MAX];
void add(int x,int y,int z){
st[nl] = node{x,y,z,0,head[x]},head[x] = nl++;
st[nl] = node{y,x,0,0,head[y]},head[y] = nl++;
}
bool BFS(int s,int e){
queue <int> q;
memset(d,-1,sizeof d);
d[s] = 0,q.push(s);
while(!q.empty()){
int o = q.front();
q.pop();
for(int i = head[o]; i != -1; i = st[i].next){
node& w = st[i];
if(d[w.to] == -1 && w.cap - w.flow > 0){
d[w.to] = d[o] + 1;
if(w.to == e) return true;
q.push(w.to);
}
}
}
return false;
}
int DFS(int s,int x,int e){
if(s == e || x == 0) return x;
int flow = 0,o;
for(int i = head[s]; i != -1; i = st[i].next){
node& w = st[i];
if(d[w.to] == d[s] + 1 && (o = DFS(w.to,min(x,w.cap - w.flow),e)) > 0){
x -= o;
flow += o;
w.flow += o;
st[i^1].flow -= o;
if(x == 0) break;
}
}
return flow;
}
int Maxflow(int s,int e){
int flow = 0;
while(BFS(s,e)){
flow += DFS(s,INF,e);
}
return flow;
}
int main()
{
int N,M;
N = read(),M = read();
init();
int s = 0,e = M * 2 + 1;
for(int i = 1; i <= M; i++){
scanf("%d",&a[i]);
for(int j = 1; j <= N; j++)
scanf("%d",&p[i][j]);
for(int j = 1; j <= N; j++)
scanf("%d",&w[i][j]);
}
for(int i = 1; i <= M; i++){
add(i,i + M,a[i]); // 接收节点和产出节点建边
int ok = 1;
for(int j = 1; j <= N; j++)
if(p[i][j] == 1)
ok = 0;
if(ok) add(s,i,INF); // 源点和接收节点建边
ok = 1;
for(int j = 1; j <= N; j++)
if(w[i][j] != 1)
ok = 0;
if(ok) add(i + M,e,INF); // 产出节点和汇点建边
for(int j = 1; j <= M; j++){
if(j == i) continue;
ok = 1;
for(int k = 1; k <= N; k++)
if(p[j][k] != 2 && p[j][k] != w[i][k])
ok = 0;
if(ok) add(i + M,j,INF); // 产出节点和匹配的接收节点建边
}
}
int ans = Maxflow(s,e),pl = 0;
for(int i = 1; i < e; i++)
for(int j = head[i]; j != -1; j = st[j].next){
node& o = st[j];
if(o.to > 0 && o.to <= M && o.flow > 0) // 产出节点和接收节点有流
sum[++pl][0] = i - M,sum[pl][1] = o.to,sum[pl][2] = o.flow;
}
printf("%d %d\n",ans,pl);
for(int i = 1; i <= pl; i++)
printf("%d %d %d\n",sum[i][0],sum[i][1],sum[i][2]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息