ACM Computer Factory
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 9189 | Accepted: 3385 | 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.
板子题,至于输出路径,在结构体里新加入一个成员,并记录是正边还是反边,最后直接看flow是否有变化,如果有变化并且是正边,那就记录下from,to,flow,并用cnt统计路径总数,再循环输出就ojbk了
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
#define cl clear()
#define pb push_back
#define sz size()
const int MAX = 1e3 + 7;
const int INF = 0x3f3f3f3f;
int in[MAX], out[MAX], ans[MAX];
int n, p, cnt, S, T;
int a[MAX][MAX], v[MAX];
struct edge{
int from, to, cap, flow, lll;
};
struct dinic{
int n, m, S, T;
vector <edge> edges;
vector <int> G[MAX];
bool vis[MAX];
int lev[MAX], cur[MAX];
void init(int n){
this->n = n;
for(int i = 0; i <= n; i++) G[i].cl;
edges.cl;
}
void add(int from, int to, int cap){
edges.pb({from, to, cap, 0, 1});
edges.pb({to, from, 0, 0, 0});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
}
bool bfs(){
memset(vis, 0, sizeof vis);
queue <int> q;
q.push(S);
lev[S] = 0, vis[S] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i = 0; i < G[x].sz; i++){
edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
lev[e.to] = lev[x] + 1;
q.push(e.to);
}
}
}
return vis[T];
}
int dfs(int x, int a){
if(x == T || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i < G[x].sz; i++){
edge &e = edges[G[x][i]];
if(lev[x] + 1 == lev[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
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;
}
void solve(int s, int t){
int flow = maxflow(s, t);
cout << flow;
}
} A;
void MAP(){
S = n + 1, T = n + 2;
for(int i = 1; i <= n; i++){
int flag = 1;
for(int j = 0; j < p; j++) if(a[i][j] == 1) flag = 0;
if(flag == 1) A.add(S, i, v[i]);
flag = 1;
for(int j = p; j < 2 * p; j++) if(a[i][j] == 0) flag = 0;
if(flag == 1) A.add(i, T, v[i]);
}
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
int flag = 0;
if(i == j) continue;
for(int k = 0; k < p; k++) if(a[i][k + p] + a[j][k] == 1) flag = 1;
if(flag == 0) A.add(i, j, v[i]);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> p >> n;
A.init(n + 2);
for(int i = 1; i <= n; i++){
cin >> v[i];
for(int j = 0; j < p * 2; j++) cin >> a[i][j];
}
MAP();
n += 2;
A.solve(S, T);
int cnt = 0;
memset(in, 0, sizeof in);
memset(out, 0, sizeof out);
memset(ans, 0, sizeof ans);
for(int i = 0; i < A.edges.sz; i++){
if(A.edges[i].from == S || A.edges[i].from == T || A.edges[i].to == S || A.edges[i].to == T) continue;
if(A.edges[i].lll == 1){
if(A.edges[i].flow > 0){
in[cnt] = A.edges[i].from;
out[cnt] = A.edges[i].to;
ans[cnt] = A.edges[i].flow;
cnt++;
}
}
}
cout << " " << cnt << endl;
for(int i = 0; i < cnt; i++)
cout << in[i] << " " << out[i] << " " << ans[i] << endl;
return 0;
}