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,1Si,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
总结:
搞懂题意以后就是很明显的最大流问题,但还是有一些问题,一个就是给定的流量都是限制节点的而不是边的,所以我们先要寻找规律,经过几次尝试以后发现,任意两个节点之间的边的流量应该是起点和终点节点流量上线的最小值,这是一点,还有一点就是这道题有许多的起点和终点,所以我们用那个图论中常用的多起点多终点的套路,虚拟出来一个终点和起点,就好了,至于最后怎么输出任意两点之间的流量,可以看反向边的流量
// // main.cpp // ACM Computer Factory // // Created by 张嘉韬 on 16/8/26. // Copyright © 2016年 张嘉韬. All rights reserved. // #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <stack> #include <queue> using namespace std; const int inf=9999999; const int maxn=60; int p,n,map[maxn][maxn],v[maxn],ormap[maxn][maxn],ans,ji[maxn][20]; int input[maxn][20],output[maxn][20]; int dfs() { int flag=0,k; stack <int> st; int vis[maxn],pre[maxn]; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); st.push(0); pre[0]=-1; while(!st.empty()) { int temp=st.top(); st.pop(); for(int i=0;i<=n+1;i++) { if(map[temp][i]&&!vis[i]) { vis[i]=1; st.push(i); pre[i]=temp; if(i==n+1) { flag=1; break; } } } } int MinFlow=inf; if(flag) { //cout<<"shit"<<endl; k=n+1; while(k!=0) { //cout<<k<<" "; MinFlow=min(map[pre[k]][k],MinFlow); //cout<<MinFlow<<" "<<endl; k=pre[k]; } k=n+1; //cout<<"shit2"<<endl; while(k!=0) { map[pre[k]][k]-=MinFlow; map[k][pre[k]]+=MinFlow; k=pre[k]; } //cout<<MinFlow<<endl; return MinFlow; } else return 0; } int isStart(int a) { for(int i=0;i<p;i++) if(input[a][i]==1) return 0; return 1; } int isEnd(int a) { for(int i=0;i<p;i++) if(output[a][i]!=1) return 0; return 1; } int legal(int a,int b) { for(int i=0;i<p;i++) { int out=output[a][i]; int in=input[b][i]; if(out&&!in) return 0; else if(!out&&in==1) return 0; } return 1; } void init() { memset(map,0,sizeof(map)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(legal(i,j)&&i!=j) map[i][j]=min(v[i],v[j]); for(int i=1;i<=n;i++) { if(isStart(i)) map[0][i]=v[i]; if(isEnd(i)) map[i][n+1]=v[i]; } for(int i=0;i<=n+1;i++) for(int j=0;j<=n+1;j++) ormap[i][j]=map[i][j]; } void print() { // printf("%d\n",ans); // for(int i=1;i<=n;i++) // for(int j=1;j<=n;j++) // if(ormap[i][j]&&map[j][i]) // printf("%d %d %d\n",i,j,map[j][i]); int cut=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(ormap[i][j]>map[i][j]) { ji[cut][0]=i; ji[cut][1]=j; ji[cut][2]=ormap[i][j]-map[i][j]; cut++; } } } printf("%d %d\n",ans,cut); for(int i=0;i<cut;i++) { printf("%d %d %d\n",ji[i][0],ji[i][1],ji[i][2]); } } void show() { for(int i=0;i<=n+1;i++) { for(int j=0;j<=n+1;j++) cout<<map[i][j]<<" "; cout<<endl; } } void displayout(int a) { for(int i=0;i<p;i++) {cout<<"("<<i<<")";cout<<output[a][i]<<" ";} cout<<endl; } void displayin(int a) { for(int i=0;i<p;i++) {cout<<"("<<i<<")";cout<<input[a][i]<<" ";} cout<<endl; } int main(int argc, const char * argv[]) { //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin); while(scanf("%d%d",&p,&n)!=EOF) { //cout<<p<<" "<<n; ans=0; for(int i=1;i<=n;i++) { scanf("%d",&v[i]); for(int j=0;j<p;j++) scanf("%d",&input[i][j]); for(int j=0;j<p;j++) scanf("%d",&output[i][j]); } // displayout(1); // displayin(3); // cout<<legal(1,3)<<endl; init(); //show(); int MinFlow=0; while((MinFlow=dfs())) ans+=MinFlow; //cout<<"shit"<<endl; print(); //cout<<"********"<<endl; } return 0; }