首页 > 代码库 > HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III
题目链接
题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,此外还可以和k个任意的男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮
思路,比HDU3081多了一个条件,此外可以和k个任意的男孩配对,转化为模型,就是多了一个结点,有两种两边的方式,一种连向可以配对的,一种连向不能配对的,此外还要保证流量算在一起,这要怎么搞呢。
其实拆点就可以了,一个女孩拆成两个点,一个连可以配,一个连不能配,源点连向其中一点,容量为mid,然后两点之间,在连一条边,连接起来,这样就能保证总流量不会超过mid了,其他都和上一题差不不多,不过这题数据比上一题大,要注意一开是预处理关系的做法
代码:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXNODE = 805; const int MAXEDGE = 200005; typedef int Type; const Type INF = 0x3f3f3f3f; struct Edge { int u, v; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow) { this->u = u; this->v = v; this->cap = cap; this->flow = flow; } }; struct Dinic { int n, m, s, t; Edge edges[MAXEDGE]; int first[MAXNODE]; int next[MAXEDGE]; bool vis[MAXNODE]; Type d[MAXNODE]; int cur[MAXNODE]; vector<int> cut; void init(int n) { this->n = n; memset(first, -1, sizeof(first)); m = 0; } void add_Edge(int u, int v, Type cap) { edges[m] = Edge(u, v, cap, 0); next[m] = first[u]; first[u] = m++; edges[m] = Edge(v, u, 0, 0); next[m] = first[v]; first[v] = m++; } bool bfs() { memset(vis, false, sizeof(vis)); queue<int> Q; Q.push(s); d[s] = 0; vis[s] = true; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = first[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (!vis[e.v] && e.cap > e.flow) { vis[e.v] = true; d[e.v] = d[u] + 1; Q.push(e.v); } } } return vis[t]; } Type dfs(int u, Type a) { if (u == t || a == 0) return a; Type flow = 0, f; for (int &i = cur[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[i^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } Type Maxflow(int s, int t) { this->s = s; this->t = t; Type flow = 0; while (bfs()) { for (int i = 0; i < n; i++) cur[i] = first[i]; flow += dfs(s, INF); } return flow; } void MinCut() { cut.clear(); for (int i = 0; i < m; i += 2) { if (vis[edges[i].u] && !vis[edges[i].v]) cut.push_back(i); } } } gao; const int N = 205; int t, n, m, k, f, g[N][N], parent[N]; int u[N * N], v[N * N]; int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); } bool judge(int mid) { int s = 0, t = n * 3 + 1; gao.init(3 * n + 2); for (int i = 1; i <= n; i++) { gao.add_Edge(s, i, mid); gao.add_Edge(i, i + n, k); gao.add_Edge(i + 2 * n, t, mid); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (g[i][j]) gao.add_Edge(i, j + 2 * n, 1); else gao.add_Edge(i + n, j + 2 * n, 1); } } return gao.Maxflow(s, t) == n * mid; } int main() { scanf("%d", &t); while (t--) { scanf("%d%d%d%d", &n, &m, &k, &f); memset(g, 0, sizeof(g)); for (int i = 1; i <= n; i++) parent[i] = i; for (int i = 0; i < m; i++) scanf("%d%d", &u[i], &v[i]); int a, b; while (f--) { scanf("%d%d", &a, &b); int pa = find(a); int pb = find(b); if (pa != pb) parent[pa] = pb; } for (int i = 0; i < m; i++) g[find(u[i])][v[i]] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) g[i][j] |= g[find(i)][j]; int l = 1, r = n + 1; while (l < r) { int mid = (l + r) / 2; if (judge(mid)) l = mid + 1; else r = mid; } printf("%d\n", l - 1); } return 0; }
HDU 3277 Marriage Match III(二分+最大流)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。