【codeforces 103E】 Buying Sets
http://codeforces.com/problemset/problem/103/E (题目链接)
题意
给出$n$个数,每个数与一个集合相关联。从其中选出最小的若干个数,选出的数的个数与这些数相关联的集合的并集大小相等。
Solution
因为保证了有完全匹配,所以跑出一个完全匹配,这样我们可以知道,如果选了某一个数,必须要选的其它数是哪些。但是问题是随着匹配的变化,这种关系会不会发生变化呢?是不会的。画画图,你会发现无论如何连边,最后都是从一个点走出去经过那些点再从一条非匹配边走回来。
所以,这很显然就是一个最小权闭合子图了,套上模板就可以AC了。
细节
边数$n^2$
代码
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#define LL long long
#define inf (1ll<<30)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int maxn=500;
int head[maxn],cnt=1,ans,clk,S,T,n,a[maxn],vis[maxn],p[maxn];
vector<int> v[maxn];
struct edge {int to,next,w;}e[maxn*maxn*2]; namespace Dinic {
int d[maxn];
void link(int u,int v,int w) {
e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
}
bool bfs() {
for (int i=S;i<=T;i++) d[i]=-1;
queue<int> q;q.push(S);d[S]=0;
while (!q.empty()) {
int x=q.front();q.pop();
for (int i=head[x];i;i=e[i].next)
if (e[i].w && d[e[i].to]<0) d[e[i].to]=d[x]+1,q.push(e[i].to);
}
return d[T]>0;
}
int dfs(int x,int f) {
if (f==0 || x==T) return f;
int w,used=0;
for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
w=dfs(e[i].to,min(e[i].w,f-used));
used+=w,e[i].w-=w,e[i^1].w+=w;
if (used==f) return used;
}
if (!used) d[x]=-1;
return used;
}
int main() {
int flow=0;
while (bfs()) flow+=dfs(S,inf);
return flow;
}
}
using namespace Dinic; bool match(int x) {
for (int i=0,j=v[x].size();i<j;i++) {
if (vis[v[x][i]]==clk) continue;
vis[v[x][i]]=clk;
if (!p[v[x][i]] || match(p[v[x][i]])) {p[v[x][i]]=x;return 1;}
}
return 0;
}
int main() {
scanf("%d",&n);
for (int x,y,i=1;i<=n;i++) {
scanf("%d",&y);
for (int j=1;j<=y;j++) scanf("%d",&x),v[i].push_back(x);
}
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++) ++clk,match(i);
S=0,T=n+1;
for (int i=1;i<=n;i++)
for (int j=0,k=v[i].size();j<k;j++) link(i,p[v[i][j]],inf);
for (int i=1;i<=n;i++) {
if (a[i]<0) link(S,i,-a[i]),ans-=a[i];
else link(i,T,a[i]);
}
ans-=Dinic::main();
printf("%d",-ans);
return 0;
}
【codeforces 103E】 Buying Sets的更多相关文章
- 【codeforces 796A】Buying A House
[题目链接]:http://codeforces.com/contest/796/problem/A [题意] 让你选一个最靠近女票的,且能买的房子; 输出你和你女票的距离; [题解] 枚举 [Num ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 799B】T-shirt buying
[题目链接]:http://codeforces.com/contest/799/problem/B [题意] 告诉你每个人喜欢的衣服的颜色; 然后告诉你每件衣服的正面和背面的颜色以及它的价格; 只要 ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【31.72%】【codeforces 604B】More Cowbell
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【15.07%】【codeforces 625A】Guest From the Past
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 【php增删改查实例】第十六节 - 用户新增
6.1工具栏 <div id="toolbar"> <a href="javascript:openDialog()" class=" ...
- LESS SCSS 预编译语言
前 言 JRedu LESS 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. LESS 为 Web 开发者带来了福音,它在 ...
- DNS之BIND使用小结(Forward转发)
之前详细介绍了DNS及其在linux下的部署过程,今天再说下DNS的BIND高级特性-forwarder转发功能.比如下面一个案例:1)已经在测试环境下部署了两台内网DNS环境,DNS的zone域名为 ...
- JAVA中使用MD5加密实现密码加密
1.新建Md5.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package c ...
- PolarCode
什么是polar code极化码 为了实现可靠的信号传输,编码学家在过去的半个多世纪提出多种纠错码技术如里所码(RS码).卷积码,Turbo码等,并在各种通信系统中取得了广泛的应用.但是以往所有实用的 ...
- 第三个Sprint ------第八天
四则运算APP开发基本完成! PC端运行结果截图 移动端(华为手机)测试截图 总结:通过这次课程设计,我体会到团队协作的重要性,也体会到了理论联系实际的意义.做一件事,要坚持不懈,不能半途而废!
- shell脚本--制作自己的服务脚本
首先注意一下,我用的环境是centos6.5,中间有一些操作和在Ubuntu上有一些地方的操作是不同的, 编写脚本 首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动.关闭或者重 ...
- 在win10开启HyperV(Pro以上版本)安装的Docker,如何远程管理其他机器(Linux或者Win)的docker容器
用k8s能直接管理吗? 不把那个容器加入集群,可以吗?
- php中获取数据 php://input、$_POST与$GLOBALS['HTTP_RAW_POST_DATA']三者的区别
$_POST 只有Coentent-Type的值为application/x-www.form-urlencoded和multipart/form-data两种类型时,$_POST才能获取到数据. $ ...
- Oracle 使用PDB 的情况下进行备份恢复的使用.
1. 关于directory: pdb 需要在container 上面创建directory才可以使用 CDB里面创建的directory是会无反应. 在PDB 里面创建: cmd 之后运行 set ...