CF 103E Buying Sets 最大权闭合子图,匹配 难度:4
http://codeforces.com/problemset/problem/103/E
这道题首先一看就很像是最大权闭合子图,但是我们可以认为现在有两种点,数字和集合点,我们需要消除数字点的影响才能直接运用最大权闭合子图.
进行二分匹配,使得每个集合都唯一匹配一个数字,买下一个集合点,则意味着该集合中所有数字的对应匹配集合点都要被买下,也就是可以建立一个新图,其中某个集合点向对应数字代表的集合点连单向边,可以证明对于任意权闭合子图中的集合点,集合中所有数字的对应匹配集合点都已经在这个权闭合子图中.对这个新图的所有价格取反,答案即最大权的负数
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=305;
const int maxm=2*maxn+maxn*maxn;
const int sups=303,supt=304;
const int inf=0x6ffffff; int n;//original aspects
int price[maxn];
int st[maxn][maxn],nst[maxn]; int mch[2*maxn];//Match
bool vis[2*maxn]; int first[maxn],elen;//maximum flow
struct edge{
int nxt,f,t,c;
}e[maxm];
int dis[maxn],gap[maxn]; bool subMatch(int s){
vis[s]=true;
for(int j=0;j<nst[s];j++){
int t=st[s][j]+n;
if(vis[t]||mch[t]==s)continue;
if(mch[t]==0||(!vis[mch[t]]&&subMatch(mch[t]))){
mch[t]=s;
mch[s]=t;
return true;
}
}
return false;
}
void Match(){
for(int i=1;i<=n;i++){
if(mch[i]==0){
memset(vis,false,sizeof(vis));
subMatch(i);
}
}
} void addedge(int f,int t,int c){
e[elen].nxt=first[f];
e[elen].f=f;
e[elen].t=t;
e[elen].c=c;
first[f]=elen++;
}
void build(){
for(int i=1;i<=n;i++){
if(price[i]>=0){
addedge(sups,i,price[i]);
addedge(i,sups,0);
}
else {
addedge(i,supt,-price[i]);
addedge(supt,i,0);
}
for(int j=0;j<nst[i];j++){
int t=mch[st[i][j]+n];
addedge(i,t,inf);
addedge(t,i,0);
}
}
}
int dfs(int s,int flow){
if(s==supt)return flow;
int mindis=n;
int tflow=flow,sub;
for(int p=first[s];p!=-1;p=e[p].nxt){
int t=e[p].t;
if(e[p].c>0){
if(dis[t]+1==dis[s]){
sub=dfs(t,min(tflow,e[p].c));
e[p].c-=sub;e[p^1].c+=sub;
tflow-=sub;
if(dis[sups]>n)return flow-tflow;
if(tflow<=0)break;
}
mindis=min(mindis,dis[t]);
}
}
if(flow==tflow){
--gap[dis[s]];
if(gap[dis[s]]==0)dis[sups]=n+1;
else{
dis[s]=mindis+1;
++gap[dis[s]];
}
}
return flow-tflow;
}
int maxflow(){
int flow=0;
gap[0]=n+2;
while(dis[sups]<=n){
flow+=dfs(sups,inf);
}
return flow;
} int main(){
int ans=0;
memset(first,-1,sizeof(first));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",nst+i);
for(int j=0;j<nst[i];j++){
scanf("%d",st[i]+j);
}
}
for(int i=1;i<=n;i++){
scanf("%d",price+i);
price[i]*=-1;
if(price[i]>=0)ans+=price[i];
} Match();
build();
ans=maxflow()-ans;
printf("%d\n",ans);
return 0;
}
CF 103E Buying Sets 最大权闭合子图,匹配 难度:4的更多相关文章
- 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)
https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...
- Solution -「最大权闭合子图」做题随笔
T1 小 M 的作物 先从简化题目入手,考虑先去掉 \(c\) 的额外收益.然后尝试将所有作物种在 \(B\), 则目前得到了 \(\sum \limits_{i = 1} ^n b_i\) 的收益. ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...
- [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...
- HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...
- HDU5855 Less Time, More profit(最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...
- HDU5772 String problem(最大权闭合子图)
题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...
- SCU3109 Space flight(最大权闭合子图)
嗯,裸的最大权闭合子图. #include<cstdio> #include<cstring> #include<queue> #include<algori ...
随机推荐
- Python学习笔记3—字符串
原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...
- Object Pascal 面向对象的特性
2 面向对象的特性 在软件系统开发过程中,结构分析技术和结构设计技术具有很多优点,但同时也存在着许多难以克服的缺点.因为结构分析技术和结构设计技术是围绕着实现处理功能来构造系统的,而在系统维护和软件升 ...
- [转]Oracle中INITRANS和MAXTRANS参数
每个块都有一个块首部.这个块首部中有一个事务表.事务表中会建立一些条目来描述哪些事务将块上的哪些行/元素锁定.这个事务表的初始大小由对象的INITRANS 设置指定.对于表,这个值默认为2(索引的IN ...
- hiho1091_clicker背包问题
问题 类似有限背包问题,题目链接:clicker 实现 #include<stdio.h> #include<cmath> #include<iostream> # ...
- commonJS — 数字操作(for Number)
for Number github: https://github.com/laixiangran/commonJS/blob/master/src/forNumber.js 代码 /** * Cre ...
- OpenCV3编程入门笔记(4)腐蚀、膨胀、开闭运算、漫水填充、金字塔、阈值化、霍夫变换
腐蚀erode.膨胀dilate 腐蚀和膨胀是针对图像中的白色部分(高亮部分)而言的,不是黑色的.除了输入输出图像外,还需传入模板算子element,opencv中有三种可以选择:矩形MORPH_RE ...
- 垂直的seekbar
看下效果: 1 package org.qianyukun.core.views; import android.content.Context; import android.graphics.Ca ...
- java 多线程8(守护线程)
比如:后台偷偷运行的那些,qq下载更新包 如果一个进程中只剩下了守护线程,那么守护线程也会死亡.. 一个线程默认都不是守护线程. 判断是否是守护线程:例:d.isDaemon(); 当一个线程随着你的 ...
- hibernate执行sql的三种方式
方式一:直接使用HibernateTemplate的find()方法,find方法支持执行hql语句 List<T> list = this.getHibernateTemplate(). ...
- 小例子(三)、winform控件的移动
程序:Do You Love Me ? 说明:就是鼠标移动到“不爱”按钮上按钮就会移动到其他地方 代码: //鼠标进入控件表面的事件MouseEnter //this.ClientSize.Width ...