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 ...
随机推荐
- 匿名内部类与equals之学习要扎实
匿名内部类是胡哥给我上的第一节课,他一直在不断强调着“逻辑与思维”的重要性, 遇到问题不能用“不知道和没学过”去逃避它,所有的不知道和没教过都源自于没见过,一定要学会去看源代码,不要人云亦云..... ...
- Prince2七大原则(4)
我们先来回顾一下,PRINCE2七大原则分别是持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第四个原则:按阶段管理. 阶段管理其实是给高层提供了项目生命周期中相对应的控 ...
- Dedecms去掉URL中a目录的方法
本文实例讲述了Dedecms去掉URL中a目录的方法.分享给大家,供大家参考.具体分析如下: 使用dedecms的朋友可能会发现自己的URL目录生成是会自动带有一个/A/目录了,那么要如何去掉URL中 ...
- Python删除指定时间的文件
import os import time import sys from xml.dom import minidom, Node from xml.dom.minidom import parse ...
- RabbitMQ 消息确认机制
消息确认机制 在之前异常处理部分就已经写了,对于consumer的异常退出导致消息丢失,可以时候consumer的消息确认机制.重复的就不说了,这里说一些不一样的. consumer的消息确认机制 当 ...
- mysql 函数 GROUP_CONCAT 单元格中最长字符串和excel导出问题
GROUP_CONCAT 使用方式GROUP_CONCAT ([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) SELECT ...
- JQuery中$.ajax()方法参数详解(转载)
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- 一个ERP项目实施工程师的若干体会
本人在多年的工作中,参与了ERP的研发和实施,对ERP有较深的认识.在这里,根据自已的实施过程中的一些经历,把自已在实践中的一些体会贡献出来和大家共享,由于时间和精力所限,内容难免有不当之处,挂一漏万 ...
- yii2 codeception程序功能测试
原文地址: http://www.360us.net/article/35.html http://blog.csdn.net/enoch612/article/details/48679069 ht ...
- IntersectionObserver API
温馨提示:本文目前仅适用于在 Chrome 51 及以上中浏览. 2016.11.1 追加,Firefox 52 也已经实现. 2016.11.29 追加,Firefox 的人担心目前规范不够稳定,未 ...