poj 2536 Gopher II 最大匹配

来源:互联网 发布:mac osx 切换root 编辑:程序博客网 时间:2024/05/22 00:34

题目

题目链接:http://poj.org/problem?id=2536

题目来源:http://www.cnblogs.com/vongang/archive/2012/02/21/2361882.html

题解

最小化死掉的人,也就是最大化存活的人。

人和洞建二分图,根据题中信息算出某人对于某个洞是否能够到达,能到达就连上边。

图上的最大匹配的规模就是最多的存活的人,n|M|就是答案。

代码

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define fi first#define se secondusing namespace std;typedef long long LL;typedef pair<int,int> PII;typedef pair<double, double> pdd;// headconst int N = 105;const int M = N;const int E = N * M;struct Gragh {    struct Edge {        int to, nxt;        Edge(int to, int nxt) : to(to), nxt(nxt) {}        Edge() {}    };    Edge e[E];    int head[N], match[M], ec;    bool vis[N];    void addEdge(int from, int to) {        e[ec] = Edge(to, head[from]);        head[from] = ec++;    }    void init(int n, int m) {        ec = 0;        for (int i = 0; i <= n; i++) head[i] = -1;        for (int i = 0; i <= m; i++) match[i] = -1;    }    bool dfs(int x) {        vis[x] = true;        for (int i = head[x]; ~i; i = e[i].nxt) {            int to = e[i].to, w = match[to];            if (w == -1 || (!vis[w] && dfs(w))) {                match[to] = x;                return true;            }        }        return false;    }    int bipatiteMatch(int n) {        int ans = 0;        for (int i = 0; i < n; i++) {            memset(vis, 0, sizeof vis);            if (dfs(i)) ans++;        }        return ans;    }};inline double sq(double x) {    return x * x;}double dis(pdd &a, pdd &b) {    return sqrt(sq(a.fi - b.fi) + sq(a.se - b.se));}Gragh g;pdd a[N], b[N];int main() {    int n, m, s, v;    while (scanf("%d%d%d%d", &n, &m, &s, &v) == 4) {        s *= v;        g.init(n, m);        for (int i = 0; i < n; i++) {            scanf("%lf%lf", &a[i].fi, &a[i].se);        }        for (int i = 0; i < m; i++) {            scanf("%lf%lf", &b[i].fi, &b[i].se);        }        for (int i = 0; i < n; i++) {            for (int j = 0; j < m; j++) {                if (dis(a[i], b[j]) > s) continue;                g.addEdge(i, j);            }        }        printf("%d\n", n - g.bipatiteMatch(n));    }    return 0;}
0 0
原创粉丝点击