ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6104 Accepted: 2113 Special Judgenode
Descriptionios
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.markdown
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.app
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.ide
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.ui
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.this
The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.spa
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.code
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.orm
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.
Source
Northeastern Europe 2005, Far-Eastern Subregion
題意很差懂啊,看了許多的博客,才漸漸的理解題意了,但大多數的博客都須要拆點,但總感受不須要,就寫了一個簡單的Dinic,就AC,是否是數據水啊.
1 #include <map> 2 #include <list> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <string> 7 #include <cstdio> 8 #include <climits> 9 #include <cstring> 10 #include <cstdlib> 11 #include <iostream> 12 #include <algorithm> 13 using namespace std; 14 #define LL long long 15 #define PI acos(-1.0) 16 #define MMM 0x3f3f3f3f 17 #define RR freopen("input.txt","r",stdin) 18 #define WW freopen("output.txt","w",stdout) 19 20 const int INF = 0x3f3f3f3f; 21 22 struct node 23 { 24 int peed; 25 int in[15]; 26 int out[15]; 27 } Point[55]; 28 int n,m; 29 int s,t; 30 int Map[55][55]; 31 int Flow[55][55]; 32 bool vis[55]; 33 bool sign[55][55]; 34 bool BFS() 35 { 36 memset(vis,false,sizeof(vis)); 37 memset(sign,false,sizeof(sign)); 38 queue<int>Q; 39 Q.push(s); 40 vis[s]=true; 41 while(!Q.empty()) 42 { 43 int u=Q.front(); 44 Q.pop(); 45 for(int i=0;i<=t;i++) 46 { 47 if(!vis[i]&&Map[u][i]) 48 { 49 sign[u][i]=true; 50 vis[i]=true; 51 Q.push(i); 52 } 53 } 54 } 55 return vis[t]; 56 } 57 int DFS(int star,int num) 58 { 59 if(star==t||num==0) 60 { 61 return num; 62 } 63 int s=0; 64 int ant; 65 for(int i=0;i<=t;i++) 66 { 67 if(sign[star][i]&&(ant=DFS(i,min(Map[star][i],num)))>0) 68 { 69 Map[star][i]-=ant; 70 Map[i][star]+=ant; 71 Flow[star][i]+=ant; 72 Flow[i][star]-=ant; 73 num-=ant; 74 s+=ant; 75 if(num==0) 76 { 77 break; 78 } 79 } 80 } 81 return s; 82 } 83 int Dinic() 84 { 85 memset(Flow,0,sizeof(Flow)); 86 int sum=0; 87 while(BFS()) 88 { 89 sum+=DFS(0,INF); 90 } 91 return sum; 92 } 93 int main() 94 { 95 while(~scanf("%d %d",&m,&n)) 96 { 97 s=0; 98 t=n+1; 99 memset(Map,0,sizeof(Map)); 100 for(int i=1; i<=n; i++) 101 { 102 scanf("%d",&Point[i].peed); 103 for(int j=1; j<=m; j++) 104 { 105 scanf("%d",&Point[i].in[j]); 106 } 107 for(int j=1; j<=m; j++) 108 { 109 scanf("%d",&Point[i].out[j]); 110 } 111 } 112 bool flag; 113 for(int i=1;i<=n;i++) 114 { 115 flag=false; 116 for(int j=1;j<=m;j++) 117 { 118 if(Point[i].in[j]==1) 119 { 120 flag=true; 121 break; 122 } 123 } 124 if(!flag) 125 { 126 Map[s][i]=Point[i].peed; 127 } 128 flag=false; 129 for(int j=1;j<=m;j++) 130 { 131 if(Point[i].out[j]!=1) 132 { 133 flag=true; 134 break; 135 } 136 } 137 if(!flag) 138 { 139 Map[i][t]=Point[i].peed; 140 } 141 } 142 for(int i=1;i<=n;i++) 143 { 144 for(int j=1;j<=n;j++) 145 { 146 if(i!=j) 147 { 148 flag=false; 149 for(int k=1;k<=m;k++) 150 { 151 if(Point[i].out[k]!=Point[j].in[k]&&Point[j].in[k]!=2) 152 { 153 flag=true; 154 break; 155 } 156 } 157 if(!flag) 158 { 159 Map[i][j]=min(Point[i].peed,Point[j].peed); 160 } 161 } 162 } 163 } 164 int sum=Dinic(); 165 int num=0; 166 for(int i=1;i<=n;i++) 167 { 168 for(int j=1;j<=n;j++) 169 { 170 if(Flow[i][j]>0) 171 { 172 num++; 173 } 174 } 175 } 176 cout<<sum<<" "<<num<<endl; 177 for(int i =1;i<=n;i++) 178 { 179 for(int j=1;j<=n;j++) 180 { 181 if(Flow[i][j]>0) 182 { 183 cout<<i<<" "<<j<<" "<<Flow[i][j]<<endl; 184 } 185 } 186 } 187 } 188 return 0; 189 }