Poj_3436 ACM computer machine (Network Stream)

Source: Internet
Author: User

Refer to the Code on the courseware. The idea is to create a super source point and a super sink point. If the input string is 0000 .... or 02... is connected to the Source Vertex. If the input string is 1111... it is connected to a super sink.

Split the point and regard the status from before entering the machine to the status after the machine as a stream. The maximum number of streams is the number of components that the machine can process per hour. Then use the ek optimization -- dinic. The idea is to first use BFs to mark all nodes in the graph as layers in the traversal order, so that after finding an augmented path, you do not need to return to node 0, instead, the capacity of the next node on the augmented path is the node with the minimum capacity. ,

Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int M = 110;
const int inf = 0x6fffffff;

struct machine {
int l;
int in[11];
int out[11];
}mach[M];

int g[M][M];
int fg[M][M];
int layer[M];
bool vis[M];
int P, N, T;

bool Equal(int *a, int *b, int P) {
for(int i = 0; i < P; i++) {
if(a[i] + b[i] == 1) return false;
}
return true;
}

bool Layer() {
deque<int> q;
int i, v;
memset(layer, -1, sizeof(layer));
q.push_back(0);
layer[0] = 1;
while( !q.empty()) {
v = q.front();
q.pop_front();
for(i = 0; i <= T; i++) {
if(g[v][i] > 0 && layer[i] == -1) {
layer[i] = layer[v] + 1;
if(i == T) {q.clear(); return true;}
else q.push_back(i);
}
}
}
return false;
}

int Dinic() {
int i, v, sum = 0, Min, Min_s, s, e;

deque<int> q;
while(Layer()) {
memset(vis, 0, sizeof(vis));
vis[0] = true;
q.push_back(0);
while(!q.empty()) {
v = q.back();
if(v == T) {
Min = inf;
for(i = 1; i < q.size(); i++) {
s = q[i-1];
e = q[i];
if(g[s][e] > 0 && Min > g[s][e]) {
Min = g[s][e];
Min_s = s;
}
}
sum += Min;
for(i = 1; i < q.size(); i++) {
s = q[i-1];
e = q[i];
if(g[s][e] > 0) {
g[s][e] -= Min;
g[e][s] += Min;
}
}
while(!q.empty() && q.back() != Min_s) {
vis[q.back()] = false;
q.pop_back();
}
} else {
for(i = 0; i <= T; i++) {
if(g[v][i] > 0 && layer[i] == layer[v] + 1 && !vis[i]) {
vis[i] = true;
q.push_back(i);
break;
}
}
if(i > T) q.pop_back();
}
}
}
return sum;
}

int main() {
//freopen("data.in", "r", stdin);

int i, j, k, sum, edg;
while(~scanf("%d%d", &P, &N)) {
memset(g, 0, sizeof(g));
memset(mach, 0, sizeof(mach));
for(i = 1; i <= N; i++) {
scanf("%d", &mach[i].l);
for(j = 0; j < P; j++)
scanf("%d", &mach[i].in[j]);
for(j = 0; j < P; j++)
scanf("%d", &mach[i].out[j]);
g[i][i+N] = mach[i].l;
}
T = 2 * N + 1;
for(i = 1; i <= N; i++) {
for(j = i+1; j <= N; j++) {
if(Equal(mach[i].out, mach[j].in, P))
g[i+N][j] = inf;
if(Equal(mach[j].out, mach[i].in, P))
g[j+N][i] = inf;
}
g[0][i] = inf;
for(k = 0; k < P; k++) {
if(mach[i].in[k] == 1) {
g[0][i] = 0; break;
}
}
g[i+N][T] = inf;
for(k = 0; k < P; k++) {
if(mach[i].out[k] != 1) {
g[i+N][T] = 0; break;
}
}
}
memcpy(fg, g, sizeof(g));
sum = Dinic();
edg = 0;
for(i = N+1; i < T; i++) {
for(j = 1; j <= N; j++) {
if(g[i][j] < fg[i][j])
edg++;
}
}
printf("%d %d\n", sum, edg);
for(i = N + 1; i < T; i++) {
for(j = 1; j <= N; j++) {
if(g[i][j] < fg[i][j])
printf("%d %d %d\n", i-N, j, fg[i][j] - g[i][j]);
}
}
}
return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.