标签: 最大流

[USACO07OPEN]吃饭Dining 题解

[USACO07OPEN]吃饭Dining 题解

题目地址:洛谷:【P2891】[USACO07OPEN]吃饭Dining – 洛谷、BZOJ:Problem 1711. — [Usaco2007 Open]Dining吃饭、POJ:3281 — Dining、OpenJudge百练:OpenJudge – 3479:Dining

题目描述

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入输出格式

输入格式:
Line 1: Three space-separated integers: N, F, and D
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

输出格式:
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

输入输出样例

输入样例#1:

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

输出样例#1:

3

说明

One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

题解

“三分图匹配”?!
考虑把饮料的点放左边,食物的点放右边,建图为源→喜欢的饮料→牛→喜欢的食物→汇这样跑最大流。
然后你WA了。
这是因为你没有限制牛这个点内流过的流量,有可能给一只牛配了超过一套食物+饮料。因此我们要对牛拆点限流。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <queue>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF 
        : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 405, MAXM = 100005, INF = 1e9;

struct Edge {
    int to, cap, nxt;
} gra[MAXM << 1];
int head[MAXN], tot;

inline void addedge(int u, int v, int cap) {
    gra[tot] = Edge {v, cap, head[u]}; head[u] = tot++;
    gra[tot] = Edge {u, 0, head[v]}; head[v] = tot++;
}

int level[MAXN];
std::queue<int> que;

inline bool bfs(int s, int t) {
    memset(level, -1, sizeof(level));
    level[s] = 0; que.push(s);
    while(!que.empty()) {
        int u = que.front(); que.pop();
        for(int i = head[u]; ~i; i = gra[i].nxt) {
            int v = gra[i].to;
            if(gra[i].cap > 0 && level[v] == -1) {
                level[v] = level[u] + 1;
                que.push(v);
            }
        }
    }
    return level[t] != -1;
}

int cur[MAXN];
bool vis[MAXN];

inline int dfs(int u, int t, int left) {
    if(u == t || !left) 
        return left;
    int flow = 0; vis[u] = true;
    for(int &i = cur[u]; ~i; i = gra[i].nxt) {
        int v = gra[i].to;
        if(gra[i].cap > 0 && !vis[v] && level[v] == level[u] + 1) {
            int d = dfs(v, t, std::min(left, gra[i].cap));
            if(d > 0) {
                left -= d; flow += d;
                gra[i].cap -= d; gra[i ^ 1].cap += d;
                if(!left) {
                    level[u] = -1;
                    return flow;
                }
            }
        }
    }
    return flow;
}

inline int dinic(int s, int t) {
    int flow = 0;
    while(bfs(s, t)) {
        memset(vis, 0, sizeof(vis));
        memcpy(cur, head, sizeof(head));
        int f;
        while(f = dfs(s, t, INF)) {
            flow += f;
        }
    }
    return flow;
}

int n, f, d, fi, di, t, S, T;

// 1~n n+1~2n cow
// 2n+1~2n+f food
// 2n+f+1~2n+f+d drink

int main() {
    memset(head, -1, sizeof(head));
    n = readint(); f = readint(); d = readint();
    S = 2 * n + f + d + 1; T = S + 1;
    for(int i = 1; i <= n; i++) {
        addedge(i, i + n, 1);
    }
    for(int i = 1; i <= f; i++) {
        addedge(S, 2 * n + i, 1);
    }
    for(int i = 1; i <= d; i++) {
        addedge(2 * n + f + i, T, 1);
    }
    for(int i = 1; i <= n; i++) {
        fi = readint(); di = readint();
        while(fi--) {
            t = readint();
            addedge(2 * n + t, i, 1);
        }
        while(di--) {
            t = readint();
            addedge(i + n, 2 * n + f + t, 1);
        }
    }
    printf("%d", dinic(S, T));
    return 0;
}
[SDOI2015]星际战争 题解

[SDOI2015]星际战争 题解

题目地址:洛谷:【P3324】[SDOI2015]星际战争 – 洛谷、BZOJ:Problem 3993. — [SDOI2015]星际战争

题目描述

3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战。
在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai。当一个巨型机器人的装甲值减少到0或者以下时,这个巨型机器人就被摧毁了。
X军团有M个激光武器,其中第i个激光武器每秒可以削减一个巨型机器人Bi的装甲值。激光武器的攻击是连续的。
这种激光武器非常奇怪,一个激光武器只能攻击一些特定的敌人。Y军团看到自己的巨型机器人被X军团一个一个消灭,他们急需下达更多的指令。
为了这个目标,Y军团需要知道X军团最少需要用多长时间才能将Y军团的所有巨型机器人摧毁。但是他们不会计算这个问题,因此向你求助。

输入输出格式

输入格式:
第一行,两个整数,N、M。第二行,N个整数,A1、A2…AN。第三行,M个整数,B1、B2…BM。接下来的M行,每行N个整数,这些整数均为0或者1。这部分中的第i行的第j个整数为0表示第i个激光武器不可以攻击第j个巨型机器人,为1表示第i个激光武器可以攻击第j个巨型机器人。

输出格式:
一行,一个实数,表示X军团要摧毁Y军团的所有巨型机器人最少需要的时间。输出结果与标准答案的绝对误差不超过10-3即视为正确。

输入输出样例

输入样例#1:

2 2
3 10
4 6
0 1
1 1

输出样例#1:

1.300000

说明

【样例说明1】
战斗开始后的前0.5秒,激光武器1攻击2号巨型机器人,激光武器2攻击1号巨型机器人。1号巨型机器人被完全摧毁,2号巨型机器人还剩余8的装甲值;
接下来的0.8秒,激光武器1、2同时攻击2号巨型机器人。2号巨型机器人被完全摧毁。
对于全部的数据,1<=N, M<=50,1<=Ai<=10510^5105 ,1<=Bi<=1000,输入数据保证X军团一定能摧毁Y军团的所有巨型机器人

题解

这个问题本身很难办,因为一个武器无法同时攻击多个目标,这一点在常规的网络流算法中难以控制。既然如此,我们考虑转换为判定问题,判断所给时间内是否能打败。这个我们可以用最大流跑,建一个源→武器→机器人→汇的图,其中源→武器的容量为武器在这段时间内的攻击输出(即t \cdot B_i),机器人→汇的容量为机器人的装甲值。只要汇点可以满流,就说明可以打败。
至于精度问题,要求1e-3的误差,那么扩大1e4倍即可解决问题。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <queue>

typedef long long LL;

const int MAXN = 105, MAXM = 500005;
const LL INF = 1e15;
const double EPS = 1e-6;

struct Edge {
    int to; 
    LL cap;
    int nxt;
} gra[MAXM << 1];
int head[MAXN], tot;

inline void addedge(int u, int v, LL cap) {
    gra[tot] = Edge {v, cap, head[u]}; head[u] = tot++;
    gra[tot] = Edge {u, 0, head[v]}; head[v] = tot++;
}

inline void init() {
    memset(head, -1, sizeof(head));
    tot = 0;
}

int level[MAXN];
bool vis[MAXN];
std::queue<int> que;

inline bool bfs(int s, int t) {
    memset(level, -1, sizeof(level));
    level[s] = 0; que.push(s);
    while(!que.empty()) {
        int u = que.front(); que.pop();
        for(int i = head[u]; ~i; i = gra[i].nxt) {
            int v = gra[i].to;
            if(level[v] == -1 && gra[i].cap > 0) {
                level[v] = level[u] + 1;
                que.push(v);
            }
        }
    }
    return level[t] != -1;
}

inline LL dfs(int u, int t, LL left) {
    if(left == 0 || u == t) return left;
    vis[u] = true;
    LL flow = 0;
    for(int i = head[u]; ~i; i = gra[i].nxt) {
        int v = gra[i].to;
        if(!vis[v] && gra[i].cap > 0 && level[v] == level[u] + 1) {
            int d = dfs(v, t, std::min(left, gra[i].cap));
            if(d > 0) {
                gra[i].cap -= d; gra[i ^ 1].cap += d;
                flow += d; left -= d;
                if(left == 0) {
                    level[u] = -1;
                    return flow;
                }
            }
        }
    }
    return flow;
}

inline LL dinic(int s, int t) {
    LL flow = 0;
    while(bfs(s, t)) {
        memset(vis, 0, sizeof(vis));
        LL f;
        while(f = dfs(s, t, INF)) {
            flow += f;
        }
    }
    return flow;
}

LL n, m, a[MAXN], b[MAXN], S, T, sum;
bool canatt[MAXN][MAXN];

inline bool check(LL mid) {
    init();
    for(int i = 1; i <= n; i++) {
        addedge(S, i, a[i]);
    }
    for(int i = 1; i <= m; i++) {
        addedge(i + n, T, b[i] * mid);
    }
    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= n; j++) {
            if(canatt[i][j]) addedge(j, i + n, INF);
        }
    }
    LL flow = dinic(S, T);
    return flow >= sum;
}

int main() {
    scanf("%lld%lld", &n, &m);
    S = n + m + 1; T = S + 1;
    for(int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        a[i] *= 1e4;
        sum += a[i];
    }
    for(int i = 1; i <= m; i++) {
        scanf("%lld", &b[i]);
    }
    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= n; j++) {
            scanf("%d", &canatt[i][j]);
        }
    }
    LL l = 0, r = 1e9, mid;
    while(r - l > 1) {
        mid = (l + r) >> 1; 
        if(check(mid)) r = mid; else l = mid;
    }
    printf("%.4lf", r / 1e4);
    return 0;
}
最大流的三种算法:Ford-Fulkson、Edmons-Karp、Dinic

最大流的三种算法:Ford-Fulkson、Edmons-Karp、Dinic

这里不介绍算法,只提供模板代码。所有代码基于【P3376】【模板】网络最大流 – 洛谷一题。

Ford-Fulkson

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
bool vis[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Ford-Fulkerson DFS
inline int dfs(int u, int t, int f) {
    if(u == t) return f;
    vis[u] = true;
    for(int i = 0; i < vec[u].size(); i++) {
        int v = vec[u][i].to;
        if(!vis[v] && vec[u][i].cap > 0) {
            int nxt = dfs(v, t, std::min(f, vec[u][i].cap));
            if(nxt > 0) {
                vec[u][i].cap -= nxt;
                vec[v][vec[u][i].rev].cap += nxt;
                return nxt;
            }
        } 
    }
    return 0;
}

inline int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
        memset(vis, 0, sizeof vis);
        int f = dfs(s, t, INF);
        if(f == 0) return flow;
        flow += f;
    }
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}

Edmons-Karp

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility> 
typedef std::pair<int, int> pairi;

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
std::queue<int> que;
int f[MAXN];
pairi pre[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Edmons-Karp BFS

inline int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
        memset(f, 0, sizeof f);
        while(!que.empty()) que.pop();
        que.push(s);
        f[s] = INF;
        while(!que.empty()) {
            int u = que.front();
            que.pop();
            for(int i = 0; i < vec[u].size(); i++) {
                int v = vec[u][i].to;
                if(f[v] == 0 && vec[u][i].cap > 0) {
                    pre[v] = std::make_pair(u, i);
                    f[v] = std::min(f[u], vec[u][i].cap);
                    que.push(v);
                }
            }
            if(f[t] != 0) break;
        }
        if(f[t] == 0) break;
        for(int u = t; u != s; u = pre[u].first) {
            vec[pre[u].first][pre[u].second].cap -= f[t];
            vec[u][vec[pre[u].first][pre[u].second].rev].cap += f[t];
        }
        flow += f[t];
    }
    return flow;
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}

Dinic

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
std::queue<int> que;
int level[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Dinic 

inline bool bfs(int s, int t) {
    memset(level, -1, sizeof level);
    level[s] = 0;
    que.push(s);
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        for(int i = 0; i < vec[u].size(); i++) {
            int v = vec[u][i].to;
            if(level[v] == -1 && vec[u][i].cap > 0) {
                level[v] = level[u] + 1;
                que.push(v);
            }
        } 
    }
    return level[t] != -1;
}

inline int dfs(int u, int t, int left) {
    if(u == t || left == 0) return left;
    int flow = 0;
    for(int i = 0; i < vec[u].size(); i++) {
        int v = vec[u][i].to;
        if(vec[u][i].cap > 0 && level[v] == level[u] + 1) {
            int d = dfs(v, t, std::min(left, vec[u][i].cap));
            if(d > 0) {
                vec[u][i].cap -= d;
                vec[v][vec[u][i].rev].cap += d;
                flow += d;
                left -= d;
                if(left == 0) break;
            }
        }
    }
    return flow;
}

inline int max_flow(int s, int t) {
    int flow = 0;
    while(bfs(s, t)) {
        flow += dfs(s, t, INF);
    }
    return flow;
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}