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 ...
随机推荐
- [转载]VFS—Kernel Space & User Space
在了解虚拟文件系统之前 , 需要先了解 Kernel Space 和 User Space 的区别 . 二者的差别在于内存使用上安全机制的差异 . kernel 执行时会占据一段系统的内存空间 , 这 ...
- PostgreSQL数据库服务端监听设置及客户端连接方法教程
众所周知,PostgreSQL 是一个自由的对象-关系数据库服务器(数据库管理系统),是一个可以免费使用的开放源代码数据库系统.本文详细介绍了PostgreSQL数据库服务端监听设置及客户端连接方法, ...
- thinkphp分页效果的制作,按查询条件分页正确做法
PHP代码: <?php namespace Home\Controller; use Think\Controller; use Home\Clas\Cate; class IndexCont ...
- ttttttttttt
http://www.2cto.com/kf/201606/519504.html http://a67474506.iteye.com/blog/2079590 spring boot: http: ...
- VS2013和VS2008项目的互通
VS2013和VS2008项目的互通,大家可能都查到了百度经验里面的一个帖子: http://jingyan.baidu.com/article/f54ae2fc3c3adc1e92b849de.ht ...
- SO_REUSEADDR 和 SO_REUSEPORT
大部分内容来自stackoverflow上的回答:Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they m ...
- TCP 连接建立和断开,以及状态转换
1. TCP报文结构 TCP是一种可靠.面向连接.全双工的传输层协议,其报文格式如下所示: 源端口.目的端口:16位长.标识出远端和本地的端口号. 顺序号:32位长.表明了发送的数据 ...
- phalcon几种分页方法
phalcon几种分页方法 一: use Phalcon\Paginator\Adapter\Model as PaginatorModel; // Current page to show // I ...
- 悟透javascript读书笔记
1.undefined,null,0,"" 这四个值转换为逻辑值时是false,其他无论简单类型值,对象或者函数转换过来都是true 2.如图 第一个是“声明了一个变量,给变量赋 ...
- 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator
[Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...