task [最大权闭合子图]
题面

思路
其实仔细读透就发现,是一个最大权闭合子图的模型
套进网络流里面就挺好做的了
可以选择重载这道题里面的一些运算(加减,取最小值),这样比较方便
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read(){
int re=0,flag=1;char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') flag=-1;
ch=getchar();
}
while(isdigit(ch)) re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
return re*flag;
}
int n,m,tote,cnte=-1,first[10010],dep[10010],cur[10010];
struct rsc{//rsc就是resource的意思啦
int a[10];
rsc(){memset(a,0,sizeof(a));}
inline bool operator<(const rsc b){
for(int i=1;i<=m;i++) if(a[i]!=b.a[i]) return a[i]<b.a[i];
return 0;
}
inline rsc operator+(const rsc b){
rsc re;
for(int i=1;i<=m;i++) re.a[i]=a[i]+b.a[i];
return re;
}
inline rsc operator-(const rsc b){
rsc re;
for(int i=1;i<=m;i++) re.a[i]=a[i]-b.a[i];
return re;
}
inline bool zero(){
for(int i=1;i<=m;i++) if(a[i]!=0) return 0;
return 1;
}
};
rsc INF,zero;
struct edge{
int to,next;
rsc w;
}a[200010];
inline void add(int u,int v,rsc adde){
cnte++;a[cnte].to=v;a[cnte].next=first[u];a[cnte].w=adde;first[u]=cnte;
cnte++;a[cnte].to=u;a[cnte].next=first[v];a[cnte].w=zero;first[v]=cnte;
}
bool bfs(int s,int t){
int q[10010],head=0,tail=1,i,u,v;
memset(dep,-1,sizeof(dep));
for(i=0;i<=n+1;i++) cur[i]=first[i];
q[0]=s;dep[s]=0;
while(head<tail){
u=q[head++];
for(i=first[u];~i;i=a[i].next){
v=a[i].to;
if(~dep[v]||a[i].w.zero()) continue;
dep[v]=dep[u]+1;q[tail++]=v;
}
}
return ~dep[t];
}
rsc min(rsc l,rsc r){
return ((l<r)?l:r);
}
rsc dfs(int u,int t,rsc lim){
if(u==t||lim.zero()) return lim;
int i,v;rsc f,flow=zero;
for(i=cur[u];~i;i=a[i].next){
v=a[i].to;cur[u]=i;
if(dep[v]!=dep[u]+1) continue;//这个地方注意要先判,否则要是先下一句话的话就会无限循环了(其实这个主要是我的写法的锅)
f=dfs(v,t,min(a[i].w,lim));
if(!f.zero()){
a[i].w=(a[i].w-f);a[i^1].w=(a[i^1].w+f);
flow=(flow+f);lim=(lim-f);
if(lim.zero()) return flow;
}
}
return flow;
}
rsc dinic(int s,int t){
rsc re;
while(bfs(s,t)) re=(re+dfs(s,t,INF));
return re;
}
int main(){
memset(first,-1,sizeof(first));
cnte=-1;
int i,j,t1,t2;
rsc tmp,re;
char s[10];
n=read();tote=read();m=read();
for(i=1;i<=m;i++) INF.a[i]=1e9;
for(i=1;i<=n;i++){
scanf("%s",s);
memset(tmp.a,0,sizeof(tmp.a));
for(j=0;j<m;j++){
if(s[j]=='+') tmp.a[j+1]=1;
if(s[j]=='-') tmp.a[j+1]=-1;
}
if(tmp<zero){
for(j=1;j<=m;j++) tmp.a[j]=-tmp.a[j];
add(i,n+1,tmp);
}
else add(0,i,tmp),re=(re+tmp);
}
for(i=1;i<=tote;i++){
t1=read();t2=read();
add(t1,t2,INF);
}
tmp=dinic(0,n+1);
for(i=1;i<=m;i++) printf("%d ",re.a[i]-tmp.a[i]+1000);
}
task [最大权闭合子图]的更多相关文章
- 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 ...
- hiho 第119周 最大权闭合子图
描述 周末,小Hi和小Ho所在的班级决定举行一些班级建设活动. 根据周内的调查结果,小Hi和小Ho一共列出了N项不同的活动(编号1..N),第i项活动能够产生a[i]的活跃值. 班级一共有M名学生(编 ...
- [HIHO119]网络流五·最大权闭合子图(最大流)
题目链接:http://hihocoder.com/contest/hiho119/problem/1 题意:中文题意. 由于1≤N≤200,1≤M≤200.最极端情况就是中间所有边都是满的,一共有N ...
随机推荐
- BZOJ4198: [Noi2015]荷马史诗(哈夫曼树)
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1824 Solved: 983[Submit][Status][Discuss] Descripti ...
- 【树链剖分 ODT】cf1137F. Matches Are Not a Child's Play
孔爷的杂题系列:LCT清新题/ODT模板题 题目大意 定义一颗无根树的燃烧序列为:每次选取编号最小的叶子节点形成的序列. 要求支持操作:查询一个点$u$在燃烧序列中的排名:将一个点的编号变成最大 $n ...
- 转:比较spring cloud和dubbo,各自的优缺点是什么
原文:https://blog.csdn.net/u010664947/article/details/80007767 dubbo由于是二进制的传输,占用带宽会更少 springCloud是http ...
- Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程
一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...
- Shell学习——数值运算
在Bash shell中,可以利用let.(( )).[]执行基本的算术操作,在高级操作时,使用expr和bc两个工具1.let[root@client02 ~]# no1=4[root@client ...
- java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接
基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...
- Linux系统完整安装在虚拟机Mini
打开VMware Workstation虚拟机,然后如下图一步到位: 此处只是简单的安装Linux系统,要想查看安装后的IP等配置看: https://www.cnblogs.com/gentle-a ...
- 解决cmd 运行python socket怎么终止运行
在cmd里启动python写了一个socket服务端的程序,但是启动之后由于监听连接的是一个死循环 这时想终止运行,发现按ctrl+c,ctrl+z,ctrl+d都不能终止 用ctrl+break解决
- Fruits【水果】
Fruits Many of us love July because it's the month when nature's berries and stone fruits are in abu ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...