POJ3436 ACM Computer Factory(最大流/Dinic)题解
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8944 | Accepted: 3267 | 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
题意:
题意很难理解,想了半天才看懂。有n台机器,每台机器有三个参数:输入规格、输出规格、产量。输入规格有三个参数:0(不需要零件)、1(必须要零件)、2(随便)。输出规格有两个参数:0(不产出零件)、1(产出零件)。所以,对于输入规格为“012”的机器,需要输出规格为“010”或者“011”的机器与之相连。若一台机器输入规格是“000”说明他是最开始那台机器(因为不用放入零件),相同,一台机器输出规格是“111”说明他是最后那台机器(因为所有零件他都生产,组装成一台电脑)。求生产最大的产量、能生产电脑的产品线数量...
思考:
用网络流最大流,大白有模板。代码里有比较详细的解释。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=510;
const int INF=1e9;
using namespace std;
struct Edge{
int from,to,cap,flow; //cap容量,flow流量
};
struct Dinic{
int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
vector<Edge> edge; //边表
vector<int> G[N]; //邻接表,G[i][j]表示结点i的第j条边在edge中的序号
bool vis[N]; //BFS使用
int d[N]; //从s到i的举例(层数)
int cur[N]; //弧下标
void init(int n){
this->n=n;
edge.clear();
for(int i=0;i<=n;i++) G[i].clear();
}
void add(int from,int to,int cap){ //from,to,cap,flow
edge.push_back((Edge){from,to,cap,0}); //压入边
edge.push_back((Edge){to,from,0,0}); //反向弧
m=edge.size();
G[from].push_back(m-2); //边序号
G[to].push_back(m-1); //反向弧序号
}
bool bfs(){ //分层
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++){ //同一结点边遍历
Edge& e=edge[G[x][i]]; //e为当前边
if(!vis[e.to] && e.cap>e.flow){ //如果当前边未走过 && 流量还能增加
vis[e.to]=1;
d[e.to]=d[x]+1; //分层
q.push(e.to);
}
}
}
return vis[t]; //没访问t回复false
}
int dfs(int x,int a){ //a为当前所有弧最小残量
if(x==t || a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &e=edge[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)) )>0 ){ //有下一层 && 还能增广
e.flow+=f; //流量增加
edge[G[x][i]^1].flow-=f; //反向弧减少
flow+=f; //flow将每一条通路的最小残量相加
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
};
bool judge(int out[],int in[],int p){ //0-0:2 1-1:2
for(int i=0;i<p;i++){
if(out[i]!=in[i] && in[i]!=2) return false;
}
return true;
}
int main(){
int s,t,p,n,w[55],in[55][15],out[55][15];
while(~scanf("%d%d",&p,&n)){
Dinic D;
D.init(t);
s=0;t=2*n+1;
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
bool flag=true;
for(int j=0;j<p;j++){
scanf("%d",&in[i][j]);
if(in[i][j]==1) flag=false;
}
if(flag) D.add(s,i,INF); //与源点相连
flag=true;
for(int j=0;j<p;j++){
scanf("%d",&out[i][j]);
if(out[i][j]==0) flag=false;
}
if(flag) D.add(i+n,t,INF); //与汇点相连
}
for(int i=1;i<=n;i++){
D.add(i,i+n,w[i]); //内部相连
for(int j=1;j<=n;j++){
if(i==j) continue;
if(judge(out[i],in[j],p)) D.add(i+n,j,INF);
}
}
int flow=D.maxflow(s,t); //得到最大流
int cnt=0;
for(int i=0;i<D.edge.size();i++){
if(D.edge[i].from==s||D.edge[i].to==s||D.edge[i].from==t||D.edge[i].to==t)
continue;
if((D.edge[i].from+n)==D.edge[i].to||(D.edge[i].from-n)==D.edge[i].to)
continue;
if(D.edge[i].flow<0) //找到反向弧
cnt++;
}
printf("%d %d\n",flow,cnt);
for(int i=0;i<D.edge.size();i++){
if(D.edge[i].from==s||D.edge[i].to==s||D.edge[i].from==t||D.edge[i].to==t)
continue;
if((D.edge[i].from+n)==D.edge[i].to||(D.edge[i].from-n)==D.edge[i].to)
continue;
if(D.edge[i].flow<0){ //找到反向弧
cout<<D.edge[i].to-n<<" "<<D.edge[i].from<<" "<<D.edge[i].flow*(-1)<<endl;
}
}
}
return 0;
}
模板:
struct Edge{
int from,to,cap,flow; //cap容量,flow流量
};
struct Dinic{
int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
vector<Edge> edge; //边表
vector<int> G[N]; //邻接表,G[i][j]表示结点i的第j条边在edge中的序号
bool vis[N]; //BFS使用
int d[N]; //从s到i的举例(层数)
int cur[N]; //弧下标
void init(int n){
this->n=n;
edge.clear();
for(int i=0;i<=n;i++) G[i].clear();
}
void add(int from,int to,int cap){ //from,to,cap,flow
edge.push_back((Edge){from,to,cap,0}); //压入边
edge.push_back((Edge){to,from,0,0}); //反向弧
m=edge.size();
G[from].push_back(m-2); //边序号
G[to].push_back(m-1); //反向弧序号
}
bool bfs(){ //分层
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++){ //同一结点边遍历
Edge& e=edge[G[x][i]]; //e为当前边
if(!vis[e.to] && e.cap>e.flow){ //如果当前边未走过 && 流量还能增加
vis[e.to]=1;
d[e.to]=d[x]+1; //分层
q.push(e.to);
}
}
}
return vis[t]; //没访问t回复false
}
int dfs(int x,int a){ //a为当前所有弧最小残量
if(x==t || a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &e=edge[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)) )>0 ){ //有下一层 && 还能增广
e.flow+=f; //流量增加
edge[G[x][i]^1].flow-=f; //反向弧减少
flow+=f; //flow将每一条通路的最小残量相加
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
};