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;
}

转载于:https://www.cnblogs.com/MashiroSky/p/6665121.html

【codeforces 103E】 Buying Sets相关推荐

  1. 【codeforces 796A】Buying A House

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

  2. 【CodeForces - 722D】Generating Sets(二分,贪心)

    题干: You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct posit ...

  3. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

    题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...

  4. 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

    题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...

  5. 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

    题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...

  6. 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)

    题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...

  7. 【CodeForces 997C】Sky Full of Stars(组合计数)

    题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...

  8. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  9. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

最新文章

  1. 【Ubuntu】Ubuntu下的录频软件SimpleScreenRecorder
  2. floyd 判圈算法 UVa 11549 计算器谜题
  3. html重复标题,在HTML中重复表标题
  4. Android跟web哪个好,比系统自带的WebView更好用 | AgentWeb
  5. macOS 新功能:【控制中心】让你的 Mac 系统更方便!
  6. 如何做好计算机病毒的查杀,电脑中的木马病毒如何彻底查杀?
  7. vue 后台翻译_vue实现在线翻译功能
  8. HTML跳转下一行快捷键,wps常用快捷键有哪些?
  9. NetFlix 服务注册与发现 Eureka
  10. 简单实现Rectrofit+RXJAVA+Fresco
  11. 最新封装版EVE模拟器部署和使用说明(图文版)
  12. upyun java_又拍云Java SDK
  13. mysql 分区表合并_MySQL 合并表、分区表
  14. 论文复现-1:Perturbation CheckLists for Evaluating NLG Evaluation Metrics
  15. python画椭圆形_Python3 tkinter基础 Canvas create_rectangle 画虚边的矩形 create_oval 画椭圆形 圆形...
  16. Compilation error 未完待续
  17. iOS开发学无止境 - 6个iOS图片文本设计的小技巧
  18. 通过 railway 和 code-server 搭建网页版的 visual studio code
  19. 人工智能对电销产生革命性影响吗
  20. oCPC实践录 | 重新理解oCPC之量化流量价值

热门文章

  1. html右缩进怎么设置,WPS中怎么设置右缩进两个字符?
  2. mysql中字典值怎么添加_插入Python字典中的值,包括MySQL的键
  3. linux带宽最小的远程桌面,【图片】linux下哪种远程桌面服务最快?_linux吧_百度贴吧...
  4. php获取网页js中的json,从php获取json数据使用js读取显示到网页笔记
  5. PHP在浏览器中被拒绝请求,php控制请求页面浏览器缓
  6. ORA-01843:无效的月份
  7. STM32F1笔记(十三)SPI
  8. Java LocalDate类| parse()方法与示例
  9. Java Vector insertElementAt()方法与示例
  10. 输入一字符串,统计其中有多少个单词(单词之间用空格分隔)(java)