Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8931 | Accepted: 3259 | 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
分析:题意有点难懂。
题目给出N台机器加工电脑,每台电脑由P个零件组成,对于第i台机器,每小时最多可加工Qi台半成品电脑,
加工前的电脑半成品状态必须为(P1,P2,...Pk),k==P,例如加工前半成品为(0,2,1),(P=3),意思为加工前
半成品必须不需要第1个零件,第二个零件可有可无,必须有第三个零件。
加工完成后的电脑状态也像上面那样表示(但是没有状态为2的零件)。
参考:https://hk.saowen.com/a/14f1457c438900b2ec96ee6ede8f75d15fc6d6bb9d4dd1b17332fb62c7441d60
网络流模型:
1.添加一个原点S,S提供最初的原料 00000...(可以含有2)
2. 添加一个汇点T, T接受最终的产品 11111...(必须全部为1)
3. 将每个机器拆成两个点: 编号为i的接收节点,和编号为i+N的产出节点(n是机器数目),前者用于接收原料,后者用于提供加工后的半成品或成品。这两个点之间要连一条边,容量为单位时间产量Qi
4. 源点S 连边到所有接收 "0000..." 或 "若干个0及若干个2" 的机器,容量为INF
5.如果第i台机器生产完之后的零件状态与第j台机器生产之前的零件状态相互吻合,就从i+N向j连一条流量为INF的边
6. 能产出成品的节点,连边到T,容量INF
7. 求S到T的最大流
EK算法:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define INF 999999999 using namespace std; int N,P;//N台机器,有2N个点,每台电脑有P个部件 int map1[105][105],map[105][105]; int T;//T表示超级汇点,0表示超级源点 int input[52][11],output[52][11]; int vis[104],p[104]; struct Node{ int s,t,c; }a[1000]; int EK() { int ans=0;//ans表示最大流,初始化为0 while(true) { queue<int> q;//寻找增广路 memset(p,-1,sizeof(p)); memset(vis,false,sizeof(vis)); q.push(0);vis[0]=true; //printf("ans%d ",ans); while(!q.empty()) { int u=q.front(); if(u==T) break; q.pop(); for(int i=0;i<=T;i++) { if(map[u][i]&&!vis[i])//当前边容量非零,且增广点未标记 { vis[i]=true; p[i]=u;//记录点i的前一个结点v q.push(i); } } } if(p[T]==-1) break;//没有找到增广路 int k=T,MAX=INF;//MAX为增广路中的最大流 while(p[k]!=-1) { MAX=min(MAX,map[p[k]][k]); k=p[k]; } ans+=MAX;//累加进最大流 k=T;//修改路径上的边容量 while(p[k]!=-1) { map[p[k]][k]-=MAX; map[k][p[k]]+=MAX; k=p[k]; } } return ans; } int main() { scanf("%d%d",&P,&N); T=N+N+1; for(int i=1;i<=N;i++) { int Q;//第i台机器最多能同时加工Q台电脑 scanf("%d",&Q); map[i][i+N]=Q;//一台机器拆成两个点,它们之间边的容量表示每小时加工量上限 //对于机器i,点i为输入,点i+N为输出 for(int j=0;j<P;j++) scanf("%d",&input[i][j]); for(int j=0;j<P;j++) scanf("%d",&output[i][j]); } for(int i=1;i<=N;i++)//机器跟机器连边 {//双重循环,对比任意两台机器的输入跟输出是否对应 for(int j=1;j<=N;j++) { if(i==j) continue; //机器i的输出跟机器j的输入是否匹配 int flag=0; for(int k=0;k<P;k++) { if(input[j][k]==output[i][k]||input[j][k]==2) continue; if(input[j][k]!=output[i][k]) {flag=1;break;}//机器i的输出没有机器j所要求的输入 } if(!flag) map[i+N][j]=INF; } } for(int i=1;i<=N;i++)//源点跟机器输入连边 { int flag=0; for(int k=0;k<P;k++) if(input[i][k]==1) {flag=1;break;} if(!flag) map[0][i]=INF; } for(int i=1;i<=N;i++)//机器输出跟汇点连边 { int flag=0; for(int k=0;k<P;k++) if(output[i][k]==0) {flag=1;break;} if(!flag) map[i+N][T]=INF; } //复制图 for(int i=0;i<=T;i++) for(int j=0;j<=T;j++) map1[i][j]=map[i][j]; int ans=EK(); int cnt=0;//输出路径 for(int i=1;i<=N;i++) { for(int j=i+1;j<=N;j++) { if(map[i+N][j]<map1[i+N][j])//跟原图对比 { a[cnt].s=i; a[cnt].t=j; a[cnt].c=map1[i+N][j]-map[i+N][j]; cnt++;continue;//避免重复 } //注意i和j的方向,不能弄反 if(map[j+N][i]<map1[j+N][i]) { a[cnt].s=j; a[cnt].t=i; a[cnt].c=map1[j+N][i]-map[j+N][i]; cnt++; } } } printf("%d %d ",ans,cnt); for(int i=0;i<cnt;i++) printf("%d %d %d ",a[i].s,a[i].t,a[i].c); return 0; }
Dinic算法:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define INF 999999999 using namespace std; int N,P;//N台机器,有2N个点,每台电脑有P个部件 int map1[105][105],map[105][105]; int T;//T表示超级汇点,0表示超级源点 int input[52][11],output[52][11]; int dis[104]; struct Node{ int s,t,c; }a[1000]; int bfs() { memset(dis,-1,sizeof(dis)); dis[0]=0; queue<int> q; q.push(0); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<=T;i++) if(dis[i]==-1&&map[u][i]>0) { dis[i]=dis[u]+1; q.push(i); } } if(dis[T]>-1) return 1; return 0; } int dfs(int cur,int m) { if(cur==T) return m; int f,res=0; for(int i=0;i<=T;i++) { if(dis[i]==dis[cur]+1&&map[cur][i]>0&&(f=dfs(i,min(m,map[cur][i])))) { map[cur][i]-=f; map[i][cur]+=f; res+=f; m-=f; if(!m) break; } } if(res) return res; dis[cur]=-1; return 0; } int main() { scanf("%d%d",&P,&N); T=N+N+1; for(int i=1;i<=N;i++) { int Q;//第i台机器最多能同时加工Q台电脑 scanf("%d",&Q); map[i][i+N]=Q;//一台机器拆成两个点,它们之间边的容量表示每小时加工量上限 //对于机器i,点i为输入,点i+N为输出 for(int j=0;j<P;j++) scanf("%d",&input[i][j]); for(int j=0;j<P;j++) scanf("%d",&output[i][j]); } for(int i=1;i<=N;i++)//机器跟机器连边 {//双重循环,对比任意两台机器的输入跟输出是否对应 for(int j=1;j<=N;j++) { if(i==j) continue; //机器i的输出跟机器j的输入是否匹配 int flag=0; for(int k=0;k<P;k++) { if(input[j][k]==output[i][k]||input[j][k]==2) continue; if(input[j][k]!=output[i][k]) {flag=1;break;}//机器i的输出没有机器j所要求的输入 } if(!flag) { map[i+N][j]=INF; } } } for(int i=1;i<=N;i++)//源点跟机器输入连边 { int flag=0; for(int k=0;k<P;k++) if(input[i][k]==1) {flag=1;break;} if(!flag) map[0][i]=INF; } for(int i=1;i<=N;i++)//机器输出跟汇点连边 { int flag=0; for(int k=0;k<P;k++) if(output[i][k]==0) {flag=1;break;} if(!flag) map[i+N][T]=INF; } //复制图 for(int i=0;i<=T;i++) for(int j=0;j<=T;j++) map1[i][j]=map[i][j]; int ans=0,res; while(bfs()) { while(res=dfs(0,INF)) ans+=res; } //printf("ans=%d ",ans); int cnt=0;//输出路径 for(int i=1;i<=N;i++) { for(int j=i+1;j<=N;j++) { if(map[i+N][j]<map1[i+N][j]) { a[cnt].s=i; a[cnt].t=j; a[cnt].c=map1[i+N][j]-map[i+N][j]; cnt++;continue; } if(map[j+N][i]<map1[j+N][i]) { a[cnt].s=j; a[cnt].t=i; a[cnt].c=map1[j+N][i]-map[j+N][i]; cnt++; } } } printf("%d %d ",ans,cnt); for(int i=0;i<cnt;i++) printf("%d %d %d ",a[i].s,a[i].t,a[i].c); return 0; } /* 加一组测试数据 3 27 5361 1 0 1 0 1 1 6146 0 1 0 0 0 1 4141 1 2 0 0 0 1 5481 2 1 2 1 0 0 3740 2 2 1 0 0 1 1473 2 0 0 0 0 1 1734 2 0 2 1 0 1 5668 2 0 1 1 0 0 320 1 1 2 0 0 0 564 0 1 1 1 0 1 2749 0 0 2 0 1 1 7990 2 1 2 0 0 1 1042 1 1 2 1 1 0 2057 0 2 0 1 0 1 8022 1 2 2 1 1 1 664 1 1 0 1 1 0 797 1 0 1 1 0 0 5261 0 0 2 1 1 1 557 0 2 0 1 1 1 4544 0 1 0 1 1 1 4321 0 2 2 1 1 0 3596 1 1 1 1 0 0 7274 0 1 1 0 1 0 7264 2 2 2 0 1 1 9165 1 1 2 1 1 1 4296 2 2 2 0 1 0 6489 2 0 1 1 0 0 输出: 22705 11 4 15 2758 6 8 1473 7 15 1734 8 15 1473 11 4 2501 11 23 248 14 15 2057 21 25 4321 23 20 248 24 4 257 26 20 4296 */