HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法。
题意:输入n个点,m条边的无向图。点权为负,边权为正,点权为代价,边权为获益,输出最大获益。
(最大权闭合子图:图中各点的后继必然也在图中)
构图攻略:将边看做点,
若选某条边e[i](u,v,w),则必须选点u,v。由此构成一个有向图。也符合最大权闭合子图模型。
对原本的边e[i](u,v,w)连3条边(S,n+i,w),(n+i,u,inf),(n+i,v,inf)。
对原本的点v,连1条边(v,T,p[v])。
即正权点与源点连,负权点与汇点连。
求最大流,记所有边的正权和为sum,则sum-maxflow就是答案。
显然,sap图的点有n+m+2,边有(n+m*3)*2。
具体证明推导请移步前辈的论文或者别的网站也有很详细的介绍和步骤。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std; #define ll long long
#define MP make_pair #define mxn 56000
#define mxe (51000*4*2)
#define inf 1e9
#define eps 1e-8 struct SAP{
int dis[mxn],pre[mxn],gap[mxn],arc[mxn];
int f[mxe],cap[mxe];
int head[mxn],nxt[mxe],vv[mxe],e;
void init(){e=0;memset(head,-1,sizeof(head));}
void add(int u,int v,int c){
vv[e]=v,cap[e]=c,nxt[e]=head[u],head[u]=e++;
vv[e]=u,cap[e]=0,nxt[e]=head[v],head[v]=e++;
}
ll max_flow(int s,int t,int n){
int q[mxn],j,mindis;
ll ans=0;
int ht=0,tl=1,u,v;
int low;
bool found,vis[mxn];
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memset(vis,0,sizeof(vis));
memset(arc,-1,sizeof(arc));
memset(f,0,sizeof(f));
q[0]=t;vis[t]=true;dis[t]=0;gap[0]=1;
while(ht<tl){
u=q[ht++];
for(int i=head[u];i!=-1;i=nxt[i]){
v = vv[i];
if(!vis[v]){
vis[v]=true;
dis[v]=dis[u]+1;
q[tl++]=v;
gap[dis[v]]++;
arc[v]=head[v];
}
}
}
u=s;low=inf;pre[s]=s;
while(dis[s]<n){
found=false;
for(int &i=arc[u];i!=-1;i=nxt[i]){
if(dis[vv[i]]==dis[u]-1 && cap[i]>f[i]){
found=true;v=vv[i];
low=min(low,cap[i]-f[i]);
pre[v]=u;u=v;
if(u==t){
while(u!=s){
u=pre[u];
f[arc[u]]+=low;
f[arc[u]^1]-=low;
}
ans+=low;low=inf;
}
break;
}
}
if(found) continue;
mindis=n;
for(int i=head[u];i!=-1;i=nxt[i]){
if(mindis>dis[vv[i]] && cap[i]>f[i]){
mindis=dis[vv[j=i]];
arc[u]=i;
}
}
if(--gap[dis[u]]==0) return ans;
dis[u]=mindis+1;
gap[dis[u]]++;
u=pre[u];
}
return ans;
}
}sap;
int p[5050];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;++i) scanf("%d",p+i);
ll sum = 0;
sap.init();
for(int i=1;i<=m;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
sap.add(n+m+1,n+i,c);
sap.add(n+i,a,inf);
sap.add(n+i,b,inf);
sum+=c;
}
for(int i=1;i<=n;++i)
sap.add(i,n+m+2,p[i]);
ll mf = sap.max_flow(n+m+1,n+m+2,n+m+2);
printf("%I64d\n",sum-mf);
}
return 0;
}
HDU 3879 Base Station(最大权闭合子图)的更多相关文章
- HDU 3879 Base Station(最大权闭合子图)
将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...
- hdu 3879 Base Station 最大权闭合图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...
- hdu3879 Base Station 最大权闭合子图 边权有正有负
/** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...
- HDU 3879 Base Station
Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
- hdu 3917 Road constructions 最大权闭合子图
样例说明: n(城市数目) m(工程队数目) 每个工程队上交的税收 val[i] k(k个工程) xi yi ci costi , 工程队ci承包由xi到yi,政府的补贴为costi 注意 ...
- HDU 5855 Less Time, More profit 最大权闭合子图
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...
- 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 ...
随机推荐
- 2016-2017-2《程序设计与数据结构》学生博客&git@OSC
2016-2017-2<程序设计与数据结构>学生博客&git@OSC 博客园 20162301张师瑜 20162302杨京典 20162303石亚鑫 20162304张浩林 201 ...
- angularjs + fis +modJS 对于支持amd规范的组建处理(PhotoSwipe 支持,百度webUpload支持)
这不是很好的处理方式,但是能够解决问题,希望有大神推荐更好的方式. 前端模块使用angularjs + fis +modJS 开发前端应用有两个月了.总结了以下的优点: fis 自动构建,自动发布,功 ...
- Fiddler 使用备忘
快捷键 ctrl + f(session 查询,高亮) ctrl + x(清除所有 session) alt + q(定位到命令行,以下操作为命令行语句) help(查看帮助文档) select sc ...
- matlab 将多个盒图放在一张图上
1.boxplot 将多个盒图放在一张图上 x1 = normrnd(5,1,100,1)';x2 = normrnd(6,1,200,1)';X = [x1 x2];G = [zeros(size( ...
- linux进阶1
1.linux命令 1.1.find功能:在linux文件系统中,用来查找一个文件放在哪里了.举例:find /etc -name "interfaces"总结:(1)什么时候用f ...
- Charles 从入门到精通
特别说明:原文来自唐巧大神的博客 http://blog.devtang.com/2015/11/14/charles-introduction/ Charles 从入门到精通 文章目录 1. 目录及 ...
- 微信签名算法的服务端实现(.net版本)
一.概要 微信此次开放JS接口,开放了一大批api权限,即使在未认证的订阅号也可以使用图像接口,音频接口,智能接口,地理位置,界面操作,微信扫一扫等功能.要知道:以前订阅号只能接受和被动回复用户消息而 ...
- BZOJ 2462: [BeiJing2011]矩阵模板
2462: [BeiJing2011]矩阵模板 Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 915 Solved: 432[Submit][Stat ...
- 刚开始用git遇到的无法提交变更的问题
原来我在目录里打开命令行,git bash默认执行的目录是c:/users了,错误的使用了git init,把$HOME 路径下的所有文件载入 git 仓库了,删除$HOME 路径下的".g ...
- python模块(六)
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...