您的位置:首页 > 理论基础 > 计算机网络

Luogu P4014 「 网络流 24 题 」分配问题

2018-08-28 09:01 393 查看

解题思路

还是建立超级源点和超级汇点,又因为题目给出规定一个人只能修一个工件,所以建图的时候还要讲容量都设为$1$。

人的编号是$1\rightarrow n$,工件的编号是$n+1\rightarrow 2\times n$。人和超级源点连边,工件和超级汇点连边,跑一个最小费用最大流和最大费用最大流。

附上代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxnode = 208, maxedge = 40010, INF = 2147483647;
inline int read() {
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x*10 + c-'0'; c = getchar();}
return x * f;
}
int n, m, dis[maxnode], Depth[maxnode], head[maxnode], cnt = 1, s, t, Ans;
struct edge {
int nxt, u, v, w;
}ed[maxedge];
inline void addedge(int x, int y, int cap) {
ed[++cnt].nxt = head[x];
ed[cnt].v = y, ed[cnt].w = cap, ed[cnt].u = x;
head[x] = cnt;
}
inline bool BFS() {
queue<int> Q;
memset(Depth, 0, sizeof(Depth));
Depth[s] = 1, Q.push(s);
int u;
while(!Q.empty()) {
u = Q.front();
Q.pop();
for(int i=head[u]; i; i=ed[i].nxt) {
if(ed[i].w > 0 && Depth[ed[i].v] == 0) {
Depth[ed[i].v] = Depth[u] + 1;
Q.push(ed[i].v);
if(ed[i].v == t) return true;
}
}
}
return false;
}
inline int Dinic(int u, int cap) {
if(u == t) return cap;
int delta;
for(int i=head[u]; i; i=ed[i].nxt) {
if(Depth[ed[i].v] == Depth[u] + 1 && ed[i].w > 0) {
delta = Dinic(ed[i].v, min(cap, ed[i].w));
if(delta > 0) {
ed[i].w -= delta;
ed[i^1].w += delta;
return delta;
}
}
}
return 0;
}
int main() {
n = read(), m = read();
s = 0, t = n+m+1;
for(int i=1; i<=n; i++) addedge(s, i, 1), addedge(i, s, 0);
for(int i=n+1; i<=m; i++) addedge(i, t, 1), addedge(t, i, 0);
static int x, y;
while (1) {
x = read(), y = read();
addedge(x, y, 1), addedge(y, x, 0);
if(x == -1 && y == -1) break;
}
while (BFS()) Ans += Dinic(s, INF);
if(Ans) printf("%d\n", Ans);
else printf("No Solution!\n");
if(Ans) {
for(int i=2; i<=cnt; i+=2) {
if(ed[i].v != s && ed[i].v != t && ed[i^1].v != s && ed[i^1].v != t)
if(ed[i^1].w != 0)printf("%d %d\n", ed[i].u, ed[i].v);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: