【HDOJ】3277 Marriage Match III
Dinic不同实现的效率果然不同啊。
/* 3277 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x1f1f1f1f;
const int maxn = ;
const int maxm = maxn*maxn;
const int maxv = maxn*;
const int maxe = maxv*maxv*;
bool M[maxn][maxn];
int V[maxe], F[maxe], F_[maxe], nxt[maxe];
int dis[maxv], head[maxv], head_[maxv], Q[maxv];
int pre[maxn];
int X[maxm], Y[maxm];
int s, t;
int n, m; void addEdge(int u, int v, int c) {
#ifndef ONLINE_JUDGE
printf("u = %d, v = %d, c = %d\n", u, v, c);
#endif
V[m] = v;
F_[m] = c;
nxt[m] = head[u];
head[u] = m++; V[m] = u;
F_[m] = ;
nxt[m] = head[v];
head[v] = m++;
} int find(int x) {
if (x == pre[x])
return x;
return pre[x] = find(pre[x]);
} void merge(int x, int y) {
int fx = find(x);
int fy = find(y); if (fx != fy)
pre[fx] = fy;
} bool bfs() {
int l = , r = ;
int u, v, k; memset(dis, , sizeof(dis));
Q[r++] = s;
dis[s] = ; while (l < r) {
u = Q[l++];
for (k=head[u]; k!=-; k=nxt[k]) {
v = V[k];
if (!dis[v] && F[k]) {
dis[v] = dis[u] + ;
if (v == t)
return false;
Q[r++] = v;
}
}
} return true;
} int dfs(int u, int val) {
if (u==t || val==)
return val; int ret = ;
int tmp, v; for (int& k=head_[u]; k!=-; k=nxt[k]) {
v = V[k];
if (F[k] && dis[v]==dis[u]+ && (tmp=dfs(v, min(val, F[k])))>) {
F[k] -= tmp;
F[k^] += tmp;
ret += tmp;
val -= tmp;
if (val == )
break;
}
} return ret;
} int Dinic() {
int ret = , tmp; while () {
if (bfs())
break; memcpy(head_, head, sizeof(head));
while () {
tmp = dfs(s, INF);
if (tmp == )
break;
ret += tmp;
}
} return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int tt;
int c, K, f;
int x, y;
int l, r, mid;
int ans, tmp; scanf("%d", &tt);
while (tt--) {
scanf("%d %d %d %d", &n, &c, &K, &f);
rep(i, , c)
scanf("%d %d", &X[i], &Y[i]);
rep(i, , n+)
pre[i] = i;
while (f--) {
scanf("%d %d", &x, &y);
merge(x, y);
} m = ;
s = *n+;
t = s + ;
memset(head, -, sizeof(head));
memset(M, false, sizeof(M)); rep(i, , n+) {
find(i);
addEdge(s, i, );
addEdge(i+n*, t, );
}
rep(i, , n+)
addEdge(i, i+n, K); rep(i, , c) {
x = X[i];
y = Y[i];
rep(j, , n+) {
if (pre[x]==pre[j] && !M[j][y]) {
M[j][y] = true;
addEdge(j, y+n*, );
}
}
} rep(i, , n+) {
rep(j, , n+) {
if (!M[i][j]) {
addEdge(i+n, j+n*, );
}
}
} l = ;
r = n;
ans = ; while (l <= r) {
mid = (l + r) >> ;
for (int i=; i<*n; i+=) {
F[i] = F[i+] = mid;
F[i+] = F[i+] = ;
}
rep(i, *n, m)
F[i] = F_[i];
tmp = Dinic();
#ifndef ONLINE_JUDGE
printf("tmp = %d\n", tmp);
#endif
if (tmp >= mid*n) {
ans = mid;
l = mid + ;
} else {
r = mid - ;
}
} printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】3277 Marriage Match III的更多相关文章
- 【HDOJ】3416 Marriage Match IV
先求SPSS.然后遍历每条边,检查是否为最短路径的边,如果是(dis[v]==dis[u]+w)则加入到网络流中.最后Dinic最大流. /* 3416 */ #include <iostrea ...
- HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对.然后有些女孩是朋友,满足这个朋友圈里面的人.假设 ...
- HDU 3277 Marriage Match III
Marriage Match III Time Limit: 4000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...
- HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典
Marriage Match III Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【HDOJ】1406 Ferry Loading III
模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...
- 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)
[CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
随机推荐
- NUnit使用详解(一)
转载:http://hi.baidu.com/grayworm/item/38e246165aa7b9433a176e90 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一 ...
- 20160402javaweb 开发模式
开发案例: 首先,我们确定用xml文件代替数据库,便于测试 建立web工程,基本架构见下图 代码如下: 首先是javabean:User.java package com.dzq.domian; im ...
- ACM——A + B Problem (3)
Home Problems 1086 A + B Problem (3) 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:2317 ...
- SqlServer 由于未在SqlServer的此实例上安装复制组件解决方法
sqlserver2005在复制订阅时出现: “由于未在SqlServer的此实例上安装复制组件,Microsoft SQL server 无法访问这些组件,请参阅SQL Server……” 解决方法 ...
- Microsoft_Sql_Server_2008:无法对数据库执行删除,因为它正用于复制
解决办法: sp_removedbreplication'StationErp' DROP DATABASE StationErp
- iOS iTunes文件共享
众所周知苹果不允许用户查看文件,不同的应用之间文件也没有联系.从电脑上往手机传文件,也只能通过开放沙盒目录,传到对应的应用下. 有时候我们需要导入文件到应用沙盒下,或者从沙盒中导出文件.这就需要应用的 ...
- Hibernate不同DB的日期查询
Java web项目开发,ORM层用的是Hibernate,用HQL语句查询不同数据库时,日期时间比较还是有所区别的. 1.在Mysql数据库时,是这样写的: 上面是个代码拼串截图,翻译一下是这样的: ...
- FFT —— 快速傅里叶变换
问题: 已知A[], B[], 求C[],使: 定义C是A,B的卷积,例如多项式乘法等. 朴素做法是按照定义枚举i和j,但这样时间复杂度是O(n2). 能不能使时间复杂度降下来呢? 点值表示法: 我们 ...
- System V 消息队列
3.1 概述 消息队列结构: struct msqid_ds { struct ipc_perm msg_perm; //权限结构 struct msg *msg_first; //队列中第一个消息 ...
- 01_安装redhat 7.1时常出现的问题
1. 笔记本安装时出现"dev/boot does not find ",进不去安装界面. 这是因为pci设备的问题,解决方法:在出现安装选项时,按Tab进入option模式,在末 ...