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. 学习ML.NET(2): 使用模型进行预测

    训练模型 在上一篇文章中,我们已经通过LearningPipeline训练好了一个“鸢尾花瓣预测”模型, var model = pipeline.Train<IrisData, IrisPre ...

  2. 更换Ubuntu源为国内源的操作记录

    我们都知道,Ubuntu的官方源对于国内用户来说是比较慢的,可以将它的源换成国内的源(比如阿里源),这样用起来就很快了.下面记录下更换操作: 首先了解下/etc/apt/sources.list文件 ...

  3. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra

    问了学长,感觉还是很迷啊,不过懂了个大概,这个翻转操作,实质不就是在序列后面加上前面部分比如 bw | wwbwwbw  操作过后 wbwbwwbww 而 bw | wwbwwbwbw 这样我们就知道 ...

  4. MySQL存储和获取数据

    ---恢复内容开始--- 一.MySQL存储引擎? 1.引擎定义 存储引擎其实就是如何存取数据,如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系型数据苦中数据是以表格的形式,所 ...

  5. MapReduce 过程详解

    Hadoop 越来越火, 围绕Hadoop的子项目更是增长迅速, 光Apache官网上列出来的就十几个, 但是万变不离其宗, 大部分项目都是基于Hadoop common MapReduce 更是核心 ...

  6. HDU 2052 Picture

    http://acm.hdu.edu.cn/showproblem.php?pid=2052 Problem Description Give you the width and height of ...

  7. A KeyValuePair in Java

    A KeyValuePair in Java Programming & English tuble 元组 pair 对(两)元组 tuple     三元组 dozen  一打(12个).有 ...

  8. Java正则解析HTML一例

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class Test { static String tes ...

  9. [CB]IPv6 在中国 - 大规模部署进行中 进展明显

    IPv6 在中国 - 大规模部署进行中 进展明显 2019年02月04日 08:21 3078 次阅读 稿源:solidot 0 条评论 中国有着世界上最大的网民人口,但它的 IPv6 普及度却处于世 ...

  10. Oracle18c OnlyExadataVersion 安装说明

    1.根据惜分飞还有盖国强老师云和恩墨的文章, 验证了下OnlyExadataVersion的Oracle18c的数据库安装过程. 2.oracle参数修改以及创建用户, 目录, 修改.bash_pro ...