标签:
题目大意:有N台机器,每台机器能处理相应型态的电脑,处理完后,电脑将变成另一种形态。
每台机器有相应的工作限度,每次至多处理K台
现在问,在一次流水线生产中,最多可以产生多少台完整的电脑(流水线指的是在每一台机器的工作限度下)
解题思路:题目比较难理解,理解题目的话,就比较好做了
首先,将每台机器的点拆成两个点,权值为工作限度
如果机器能处理的电脑的状态全是0的话,就将其和超级源点连接,表示该机器进行第一步加工
如果机器处理完后的形态与另一台机器能处理的最初形态相同,就将其连线,表示下一台机器可以将其处理完的电脑再进一步加工
如果机器处理完后形态都为1,表示完工,将其和超级汇点相连,接着跑最大流
ISAP
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge {
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow) {}
};
int g[70][70];
struct ISAP {
int p[N], num[N], cur[N], d[N];
int t, s, n, m;
bool vis[N];
vector<int> G[N];
vector<Edge> edges;
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++) {
G[i].clear();
d[i] = INF;
}
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
d[t] = 0;
vis[t] = 1;
Q.push(t);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i] ^ 1];
if (!vis[e.from] && e.cap > e.flow) {
vis[e.from] = true;
d[e.from] = d[u] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment() {
int u = t, flow = INF;
while (u != s) {
Edge &e = edges[p[u]];
flow = min(flow, e.cap - e.flow);
u = edges[p[u]].from;
}
u = t;
while (u != s) {
edges[p[u]].flow += flow;
edges[p[u] ^ 1].flow -= flow;
u = edges[p[u]].from;
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
BFS();
if (d[s] >= n)
return 0;
memset(num, 0, sizeof(num));
memset(cur, 0, sizeof(cur));
for (int i = 0; i < n; i++)
if (d[i] < INF)
num[d[i]]++;
int u = s;
while (d[s] < n) {
if (u == t) {
flow += Augment();
u = s;
}
bool ok = false;
for (int i = cur[u]; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && d[u] == d[e.to] + 1) {
ok = true;
p[e.to] = G[u][i];
cur[u] = i;
u = e.to;
break;
}
}
if (!ok) {
int Min = n - 1 ;
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow)
Min = min(Min, d[e.to]);
}
if (--num[d[u]] == 0)
break;
num[d[u] = Min + 1]++;
cur[u] = 0;
if (u != s)
u = edges[p[u]].from;
}
}
return flow;
}
};
ISAP isap;
#define M 70
#define P 20
int in[M][P], out[M][P], f[M];
int n, m;
bool NullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (in[cur][i] == 1)
return false;
return true;
}
bool FullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (!out[cur][i])
return false;
return true;
}
bool connect(int x, int y) {
for (int i = 1; i <= n; i++)
if (out[x][i] + in[y][i] == 1)
return false;
return true;
}
void init() {
for (int i = 1; i <= m; i++) {
scanf("%d", &f[i]);
for (int j = 1; j <= n; j++)
scanf("%d", &in[i][j]);
for (int j = 1; j <= n; j++)
scanf("%d", &out[i][j]);
}
int s = 2 * m + 1;
int t = 2 * m + 2;
isap.init(t);
for (int i = 1; i <= m; i++) {
isap.AddEdge(i, i + m, f[i]);
if (NullJudge(i))
isap.AddEdge(s, i, f[i]);
if (FullJudge(i))
isap.AddEdge(i + m, t, f[i]);
for (int j = 1; j <= m; j++)
if (i != j && connect(i, j))
isap.AddEdge(i + m, j, f[i]);
}
int ans[M][P];
int flow = isap.Maxflow(s, t);
int cnt = 0;
for (int i = m + 1; i <= m + m; i++)
for (int j = 0; j < isap.G[i].size(); j++) {
int v = isap.G[i][j];
Edge &e = isap.edges[v];
if (e.flow > 0 && e.to <= m) {
ans[cnt][0] = i - m;
ans[cnt][1] = e.to;
ans[cnt][2] = e.flow;
cnt++;
}
}
printf("%d %d\n", flow, cnt);
for (int i = 0; i < cnt; i++)
printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
}
return 0;
}
EK
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow){}
};
struct EK{
vector<int> G[N];
vector<Edge> edges;
int s, t, n, m, p[N];
bool vis[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
queue<int> q;
memset(vis, 0, sizeof(vis));
vis[s] = 1;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
p[e.to] = G[u][i];
if (e.to == t)
return true;
q.push(e.to);
}
}
}
return false;
}
int Augment() {
int flow = INF, u = t;
while (u != s) {
Edge &e = edges[p[u]];
flow = min(flow, e.cap - e.flow);
u = e.from;
}
u = t;
while (u != s) {
edges[p[u]].flow += flow;
edges[p[u] ^ 1].flow -= flow;
u = edges[p[u]].from;
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
while (BFS()) {
flow += Augment();
}
return flow;
}
};
EK ek;
#define M 70
#define P 20
int in[M][P], out[M][P], f[M];
int n, m;
bool NullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (in[cur][i] == 1)
return false;
return true;
}
bool FullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (!out[cur][i])
return false;
return true;
}
bool connect(int x, int y) {
for (int i = 1; i <= n; i++)
if (out[x][i] + in[y][i] == 1)
return false;
return true;
}
void init() {
for (int i = 1; i <= m; i++) {
scanf("%d", &f[i]);
for (int j = 1; j <= n; j++)
scanf("%d", &in[i][j]);
for (int j = 1; j <= n; j++)
scanf("%d", &out[i][j]);
}
int s = 2 * m + 1;
int t = 2 * m + 2;
ek.init(t);
for (int i = 1; i <= m; i++) {
ek.AddEdge(i, i + m, f[i]);
if (NullJudge(i))
ek.AddEdge(s, i, f[i]);
if (FullJudge(i))
ek.AddEdge(i + m, t, f[i]);
for (int j = 1; j <= m; j++)
if (i != j && connect(i, j))
ek.AddEdge(i + m, j, f[i]);
}
int ans[M][P];
int flow = ek.Maxflow(s, t);
int cnt = 0;
for (int i = m + 1; i <= m + m; i++)
for (int j = 0; j < ek.G[i].size(); j++) {
int v = ek.G[i][j];
Edge &e = ek.edges[v];
if (e.flow > 0 && e.to <= m) {
ans[cnt][0] = i - m;
ans[cnt][1] = e.to;
ans[cnt][2] = e.flow;
cnt++;
}
}
printf("%d %d\n", flow, cnt);
for (int i = 0; i < cnt; i++)
printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
}
return 0;
}
Dinic
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N], cur[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++) {
G[i].clear();
}
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[u] + 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].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[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;
}
};
Dinic dinic;
#define M 70
#define P 20
int in[M][P], out[M][P], f[M];
int n, m;
bool NullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (in[cur][i] == 1)
return false;
return true;
}
bool FullJudge(int cur) {
for (int i = 1; i <= n; i++)
if (!out[cur][i])
return false;
return true;
}
bool connect(int x, int y) {
for (int i = 1; i <= n; i++)
if (out[x][i] + in[y][i] == 1)
return false;
return true;
}
void init() {
for (int i = 1; i <= m; i++) {
scanf("%d", &f[i]);
for (int j = 1; j <= n; j++)
scanf("%d", &in[i][j]);
for (int j = 1; j <= n; j++)
scanf("%d", &out[i][j]);
}
int s = 2 * m + 1;
int t = 2 * m + 2;
dinic.init(t);
for (int i = 1; i <= m; i++) {
dinic.AddEdge(i, i + m, f[i]);
if (NullJudge(i))
dinic.AddEdge(s, i, f[i]);
if (FullJudge(i))
dinic.AddEdge(i + m, t, f[i]);
for (int j = 1; j <= m; j++)
if (i != j && connect(i, j))
dinic.AddEdge(i + m, j, f[i]);
}
int ans[M][P];
int flow = dinic.Maxflow(s, t);
int cnt = 0;
for (int i = m + 1; i <= m + m; i++)
for (int j = 0; j < dinic.G[i].size(); j++) {
int v = dinic.G[i][j];
Edge &e = dinic.edges[v];
if (e.flow > 0 && e.to <= m) {
ans[cnt][0] = i - m;
ans[cnt][1] = e.to;
ans[cnt][2] = e.flow;
cnt++;
}
}
printf("%d %d\n", flow, cnt);
for (int i = 0; i < cnt; i++)
printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ - 3436 ACM Computer Factory (ISAP EK Dinic)
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/47217907