Marriage Match III HDU - 3277(二分权值 + 拆点 建边)

题意:

  只不过是hdu3081多加了k种选择

  想一下,最多能玩x轮,是不是就是每个女生能最多选x个男生

  现在题中的每个女生比3081多了k中选择   那就把女生拆点  i  i‘

  i --> i' 连一条权值为K的边  如果男女无争吵 连上i --> 男  权值为1

  有争吵 连上i' --> 男 权值为1

  

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, z, s, t, cnt, p;
int f[maxn], re[][];
int d[maxn], cur[maxn], head[maxn];
struct node
{
int u, v, c, next;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic()
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(s, INF);
}
return ans;
} int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} void build(int mid)
{
for(int i = ; i <= n; i++)
{
add(s, i, mid);
add(n + i, t, mid);
add(i, n * + i, p);
}
for(int i = ; i <= n; i++)
for(int k = n + ; k <= n * ; k++)
{
if(re[i][k]) add(i, k, );
else add(n * + i, k, );
}
} void init()
{
for(int i = ; i < maxn; i++) f[i] = i;
mem(re, );
mem(head, -);
cnt = ;
} int main()
{
int T;
rd(T);
while(T--)
{
init();
int a, b;
rd(n), rd(m), rd(p), rd(z);
s = , t = n * + ;
for(int i = ; i < m; i++)
{
rd(a), rd(b);
b += n;
re[a][b] = re[b][a] = ;
}
for(int i = ; i < z; i++)
{
rd(a), rd(b);
int r = find(a);
int l = find(b);
if(r != l) f[l] = r;
}
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)
{
if(find(i) == find(j))
for(int k = n + ; k <= n * ; k++)
re[i][k] = re[j][k] = (re[i][k] || re[j][k]);
}
}
int x = , y = ;
while(x <= y)
{
mem(head, -);
cnt = ;
int mid = x + (y - x) / ;
build(mid);
if(Dinic() == mid * n) x = mid + ;
else y = mid - ;
}
pd(y); } return ;
}
上一篇:RPi 2B UART作为调试口或者普通串口


下一篇:关于lazyload的实现原理