【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 ...
随机推荐
- Ubuntu 打包后安装提示:子进程 已安装 pre-removal 脚本 返回了错误号 1
子进程 已安装 pre-removal 脚本 返回了错误号 1或2 与 子进程 已安装 post-installation 脚本 返回了错误号 1或2 一.子进程 已安装 pre-removal ...
- [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [三] 配置式爬虫
[DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 上一篇介绍的基本的使用方式,自由度很高,但是编写的代码相对 ...
- 程序员从技术开发到项目管理PM--思维转变
对以往所做项目的经验做下总结,作为项目经理首先要对项目负责,思维要做下转变,要从项目全局角度考虑问题: 从个人成就到团队成就. 无论是做管理还是做技术,成就导向意识是优秀员工的基本素质.只有具 ...
- ceph学习
网络: ceph必须要有公共网络和集群网络: public network:负责客户端交互以及osd与mon之间的通讯 cluster network:负责osd之间的复制,均衡,回填,数据恢复等操作 ...
- SpringBoot笔记--FastJson
FastJson配置 ObjectId class ObjectIdSerializer : ObjectSerializer { override fun write(serializer: JSO ...
- TomCat 再次发布我的程序
打包成.war的步骤就不说了,之后的配置和上一次的不一样. 在Tomcat的conf下的server.xml文件中,重新配置如下 <Service name="xfwweb" ...
- centos6下redis cluster集群部署过程
一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...
- Linux内核分析 读书笔记 (第五章)
第五章 系统调用 5.1 与内核通信 1.调用在用户空间进程和硬件设备之间添加了一个中间层.该层主要作用有三个: 为用户空间提供了硬件的抽象接口. 系统调用保证了系统的稳定和安全. 实现多任务和虚拟内 ...
- Linux添加目录到环境变量以及添加sublime到环境变量
博主之前有过这种情况,就是在普通用户下su ls等命令还有效,可登陆进root用户之后这些常用的命令竟然失效了. 像这样 这问题其实很简单,但是对于不清楚环境变量的配置的同学来说也的确棘手,我之前就是 ...
- CodeIgniter 2.x和3.x修改默认控制器问题解答
首先明确一点,CodeIgniter框架的2.x和3.x版本中修改默认控制器是有一点区别的 但相同的操作都是修改application/config/routes.php $route['defaul ...