【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 ...
随机推荐
- WPF 录屏软件研发心得及思路分享(已结束开发)
最近由于工程需要开始研发基于Windows的自动录屏软件,很多细节很多功能需要处理,毕竟一个完美的录屏软件不是你随随便便就可以写出来的.首先参考了大部分的录屏软件,在研发的过程中遇到了很多的问题:比如 ...
- Oracle数据库冷备份与热备份操作梳理
Oracle数据库的备份方式有冷备份和热备份两种,针对这两种备份的实施过程记录如下: 一.Oracle冷备份 概念数据库在关闭状态下完成所有物理系统文件拷贝的过程,也称脱机备份.适合于非归档模式(即n ...
- 2016-03-22 OneZero团队 Daily Scrum Meeting
会议时间: 2016-03-22 9:33-9:57am 会议内容: 一.在原有Sprint Backlog基础上,我们加了亮点(摇一摇功能:随机选取一条记录在界面显示,以提醒主页君回忆) 需求分析图 ...
- Linux内核分析第八周总结
第八章 进程的切换和系统的一般执行过程 进程调度与进程调度的时机分析 第一种分类: I/O密集型(I/O-bound):频繁的进行I/O,通常会花费很多时间等待I/O操作的完成 CPU密集型(CPU- ...
- 小学四则运算APP 第二阶段冲刺
第一阶段实现最基本的四则运算计算,最原始的所以还没有美化 xml文件 <LinearLayout xmlns:android="http://schemas.android.c ...
- Spring配置常识
(1)数据源配置 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" ...
- C# 中颜色和名称样式对照表
WPF中的画刷也一样适用 System.Windows.Media.Brushes.名称 (如:System.Windows.Media.Brushes.AliceBlue) :first-child ...
- 搭建ssm的步骤
搭建SSM的步骤 ----------------------------- 1.创建web工程 2.把SSM做需要的所有jar导入工程中 3.web.xml 1.Springmvc的前端控制器,如果 ...
- HTML 5 placeHolder
<html> <body> <input type="text" id="idNum" placeholder="pla ...
- K Nearest Neighbor 算法
文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...