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的更多相关文章

  1. 【codeforces 796A】Buying A House

    [题目链接]:http://codeforces.com/contest/796/problem/A [题意] 让你选一个最靠近女票的,且能买的房子; 输出你和你女票的距离; [题解] 枚举 [Num ...

  2. 【53.57%】【codeforces 722D】Generating Sets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 【codeforces 799B】T-shirt buying

    [题目链接]:http://codeforces.com/contest/799/problem/B [题意] 告诉你每个人喜欢的衣服的颜色; 然后告诉你每件衣服的正面和背面的颜色以及它的价格; 只要 ...

  5. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【31.72%】【codeforces 604B】More Cowbell

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【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 ...

  8. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【33.33%】【codeforces 586D】Phillip and Trains

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Duplicate entry * for key *

    一.问题 插入数据时报错 Duplicate entry * for key * 二.分析 建表语句 CREATE TABLE `t_product_result_config` ( `id` var ...

  2. Mybatis中 collection 和 association 的区别?

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  3. Centos下Nodejs+npm环境-部署记录

    公司的一个项目上线,需要用到Nodejs和npm环境,这里记录下安装过程,方便回看同时供大家参考. 1)yum安装方式(版本比较老点,v5.12.0 + 3.8.6) 需要在centos中添加epel ...

  4. nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理

    Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...

  5. centos6.8下LNMP (nginx1.8.0+php5.6.10+mysql5.6.12) - 部署手册

    在平时运维工作中,经常需要用到LNMP应用框架.以下对LNMP环境部署记录下: 1)前期准备:为了安装顺利,建议先使用yum安装依赖库[root@opd ~]#yum install -y make ...

  6. 软件工程启程篇章:结对编程和进阶四则运算(197 & 199)

    0x01 :序言:无关的事 I wrote a sign called "Dead End" in front of myself, but love crossed it wit ...

  7. 21035218_Linux 实验三 程序破解

    20135218  姬梦馨 1:掌握NOP.JNE.JE.JMP.CMP的汇编指令的机器码. NOP:NOP指令即“空指令”.执行到NOP指令时,CPU什么也不做,仅仅当做一个指令执行过去并继续执行N ...

  8. Linux内核读书笔记第四周

    进程管理 1.进程描述符及任务结构 进程存放在叫做任务队列(task list)的双向循环链表中.链表中的每一项包含一个具体进程的所有信息,类型为task_struct,称为进程描述符(process ...

  9. Mooc总结——Linux内核分析

    朱荟潼+ 原创作品转载请注明出处 :<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 学习笔记链接汇总 第 ...

  10. Python学习笔记 -- 第五章

    模块 使用模块可以提高了代码的可维护性.其次,编写代码不必从零开始.当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Python内置的模块和来自第三方的模块: ...