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的更多相关文章

  1. 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)

    https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...

  2. Solution -「最大权闭合子图」做题随笔

    T1 小 M 的作物 先从简化题目入手,考虑先去掉 \(c\) 的额外收益.然后尝试将所有作物种在 \(B\), 则目前得到了 \(\sum \limits_{i = 1} ^n b_i\) 的收益. ...

  3. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  4. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  5. [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...

  6. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  7. HDU5855 Less Time, More profit(最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...

  8. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  9. SCU3109 Space flight(最大权闭合子图)

    嗯,裸的最大权闭合子图. #include<cstdio> #include<cstring> #include<queue> #include<algori ...

随机推荐

  1. 2014 Multi-University Training Contest 4

    1006 hdu4902 #include <iostream> #include<stdio.h> #include<vector> #include<qu ...

  2. apiCloud结合layer实现动态数据弹出层

    css /** 我的二维码 begin **/ .aui-list .wechat-media { width: 3rem; } .wechat-middle { padding: 1.3em 3.2 ...

  3. windows volume related concepts

    Volume --- 一个volume就是一个分区.  在该“物理硬盘”上创建一个或多个分区,再创建文件系统,才可以得到一个VOLUME.此时VOLUME相对于主机是一个逻辑设备. https://m ...

  4. (八)shell中的循环结构

    1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...

  5. POJ 2484 A Funny Game(神题!)

    一开始看这道博弈题的时候我就用很常规的思路去分析了,首先先手取1或者2个coin后都会使剩下的coin变成线性排列的长条,然后无论双方如何操作都是把该线条分解为若干个子线条而已,即分解为若干个子游戏而 ...

  6. maven Connection refused: connect

    现象: 本地可以访问错误提示中的地址.但使用maven无法下载jar包. 环境: 浏览器上网需要使用代理 解决方法: 设置成正常代理可以.具体方法可以下载一个代理工具.只要IE配置成能直接访问http ...

  7. 在多线程环境下使用HttpWebRequest或者调用Web Service(连接报超时问题)

    .net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows  7 下默认是2,在服务器操作 ...

  8. ajax 跨域解决 网上资料

    PHP中运用jQuery的Ajax跨域调用实现代码,需要的朋友可以参考下   可以在页面定义一个调用方法,如下: 复制代码代码如下: function getData(){ $.getJSON(&qu ...

  9. listToString

    http://www.oschina.net/code/snippet_109648_2229

  10. er6855的工作经验

    1 VIEWS里面的关系要搞清楚 里面的内容类型要理清 不要相信别人做好的事情 不要相信看到的结果 2 git rm -rf之后需要git commit提交到.git文件中正式生效 不然可能就是中间打 ...