网络流24题(02)太空飞行计划(最大流最小割)
思路:
1. 题目所传达的意思是:从中选择若干个实验,并且附带的设备都要选择。求所能所得的最大净利润;
2. 设实验所在集合为 X 集,设备所在集为 Y 集。从 s 向 X 引弧,容量为 Pi,从 Y 向 t 引弧,容量为 Ci,X 向关联的 Y 引弧,容量为 INFS;
3. 求上述网络的最大流。根据最大流求出最小割,假设 s 所在的点集为 S,则 S 中所选择的实验以及设备即是最终要选择的结果;
4. 因为 X->Y 的弧都是 INFS,所以在 S 中,选择了 x 实验,则其附带的设备一定都选择了(根据残留网络不存在增广路径,可以用反证法理解)。
5. 最终最大流的数据是:选择的设备花费值 + 不选择的实验资金值(因为最小割的的弧是满弧,所以割中的实验是不赚钱的,所以可以无视)
6. 输出的结果即为:选择的实验资金总值 + 选择的设备花费值 = SUM - 最大流。
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 210;
const int INFS = 0x3FFFFFFF;
struct edge {
int from, to, cap, flow;
edge(int _from, int _to, int _cap, int _flow)
: from(_from), to(_to), cap(_cap), flow(_flow) {}
};
class Dinic {
public:
void clearall(int n) {
this->n = n;
edges.clear();
for (int i = 0; i < n; i++)
G[i].clear();
}
void addedge(int u, int v, int cap) {
edges.push_back(edge(u, v, cap, 0));
edges.push_back(edge(v, u, 0, 0));
G[u].push_back(edges.size() - 2);
G[v].push_back(edges.size() - 1);
}
bool BFS() {
for (int i = 0; i < n; i++)
vis[i] = false, d[i] = 0;
queue<int> Q;
Q.push(s);
vis[s] = true;
while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (int i = 0; i < G[x].size(); i++) {
edge& e = edges[G[x][i]];
if (e.cap > e.flow && !vis[e.to]) {
vis[e.to] = true;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int aug) {
if (x == t || aug == 0) return aug;
int flow = 0;
for (int i = 0; i < G[x].size(); i++) {
edge& e = edges[G[x][i]];
if (d[e.to] == d[x] + 1) {
int f = DFS(e.to, min(aug, e.cap - e.flow));
if (f <= 0) continue;
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
aug -= f;
if (aug == 0) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s, this->t = t;
int flow = 0;
while (BFS()) {
flow += DFS(s, INFS);
}
return flow;
}
void getans(vector<int>& ans) {
ans.clear();
for (int i = 1; i < n; i++) {
if (vis[i]) ans.push_back(i);
}
sort(ans.begin(), ans.end());
}
private:
vector<edge> edges;
vector<int> G[MAXN];
int n, s, t, d[MAXN];
bool vis[MAXN];
};
Dinic dinic;
int pay[110], cost[110];
int main() {
freopen("shuttle.in", "r", stdin);
freopen("shuttle.out", "w", stdout);
int n, m;
scanf("%d%d", &m, &n);
int s = 0, t = m + n + 1;
dinic.clearall(m + n + 2);
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
pay[i] = x;
dinic.addedge(s, i, x);
while (true) {
char c;
do {
c = getchar();
} while (c == ' ');
ungetc(c, stdin);
if (c == 10 || c == 13) break;
scanf("%d", &x);
dinic.addedge(i, x + m, INFS);
}
}
for (int u = 1; u <= n; u++) {
scanf("%d", &cost[u]);
dinic.addedge(u + m, t, cost[u]);
}
int flow = dinic.maxflow(s, t);
vector<int> ans;
dinic.getans(ans);
int sum = 0;
for (int i = 0; i <= m; i++)
sum += pay[i];
bool first = false;
int i;
for (i = 0; i < ans.size(); i++) {
if (ans[i] > m) break;
if (!first) first = true, printf("%d", ans[i]);
else printf(" %d", ans[i]);
}
printf("\n");
first = false;
for (; i < ans.size(); i++) {
if (!first) first = true, printf("%d", ans[i] - m);
else printf(" %d", ans[i] - m);
}
printf("\n%d\n", sum - flow);
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------