博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj1497(最小割)
阅读量:6960 次
发布时间:2019-06-27

本文共 2495 字,大约阅读时间需要 8 分钟。

 

传送门:

题意:建立n个中转站,每个花费P[i],有m个用户,使用Ai和Bi中转站可获利Ci,问最终建立哪几个中转站使获利最大?

分析:根据最大权闭合图建图,用户群和中转站为带权的点集,用户群的权为收益,中转站的权为负的成本:

  1. 从源s连弧到正权值的点,容量为次正权值。
  2. 从负权值的点连弧到汇t,容量为负权值的绝对值。
  3. 在闭合图中所有的弧换成容量为oo的弧。
  4. 答案就是所有正权值的和-最小割的容量(最大流)

解释一下建图最小割的原理:用户和中转站间的权值为oo,因此最小割集中不可能有这些边,那么割边只可能为源点和用户的连边与中转站和汇点的连边,假设割后S集为我们所取的集合,那么割边为源点和用户的连边时表示不要该获利,割边为汇点和中转站的连边时表示建立该中转站,那么:最小割=不要的获利+建立中转站花费,又知:不要的获利=总获利-收取的获利,转换一下:最小割=总获利-ans(收取的获利-建立中转站的花费),因此ans=总获利sum-最小割(最大流)。

#pragma comment(linker,"/STACK:1024000000,1024000000")#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long#define mod 100000000#define inf 0x3f3f3f3f#define eps 1e-6#define N 60010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PII pair
using namespace std;inline int read(){ char ch=getchar();int x=0,f=1; while(ch>'9'||ch<'0'){ if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f;}int n,m,sum,vs,vt,tot,NV;int pre[N],cur[N],h[N],q[N];struct edge{ int u,v,w,next; edge() {} edge(int u,int v,int w,int next):u(u),v(v),w(w),next(next) {}} e[N*6];void addedge(int u,int v,int w){ e[tot]=edge(u,v,w,pre[u]); pre[u]=tot++; e[tot]=edge(v,u,0,pre[v]); pre[v]=tot++;}void init(){ memset(pre,-1,sizeof(pre)); tot=0;}/*******************dinic************************/int bfs(){ int head=0,tail=1; memset(h,-1,sizeof(h)); q[0]=vs;h[vs]=0; while(head!=tail) { int u=q[head++]; for(int i=pre[u];~i;i=e[i].next) { int v=e[i].v,w=e[i].w; if(w&&h[v]==-1) { h[v]=h[u]+1; q[tail++]=v; } } } return h[vt]!=-1;}int dfs(int u,int flow){ if(u==vt)return flow; int used=0; for(int i=cur[u];~i;i=e[i].next) { int v=e[i].v,w=e[i].w; if(h[v]==h[u]+1) { w=dfs(v,min(flow-used,w)); e[i].w-=w;e[i^1].w+=w; if(e[i].w)cur[u]=i; used+=w; if(used==flow)return flow; } } if(!used)h[u]=-1; return used;}int dinic(){ int res=0; while(bfs()) { for(int i=vs;i<=vt;i++)cur[i]=pre[i]; res+=dfs(vs,inf); } return res;}/********************dinic***********************/int u,v,w;void build(){ vs=0;vt=n+m+1;sum=0; for(int i=1;i<=n;i++) { w=read(); addedge(i+m,vt,w); } for(int i=1;i<=m;i++) { u=read();v=read();w=read(); sum+=w; addedge(i,u+m,inf);addedge(i,v+m,inf); addedge(vs,i,w); }}int main(){ while(scanf("%d%d",&n,&m)>0) { init(); build(); printf("%d\n",sum-dinic()); }}
View Code

 

转载于:https://www.cnblogs.com/lienus/p/4295691.html

你可能感兴趣的文章
关于JavaScript的浅拷贝和深拷贝
查看>>
ElementUI的Upload上传,配合七牛云储存图片
查看>>
【架构】Tomcat单机部署多应用Windows
查看>>
Django-分页
查看>>
C++类型转换:隐式类型转换、类类型转换、显示类型转换
查看>>
JS扩展篇
查看>>
Promise函数then的奥秘
查看>>
Go语言的错误处理
查看>>
一款属于自己的小程序
查看>>
docker - nginx - proxy_pass + proxy_redirect
查看>>
Dubbo Cloud Native 之路的实践与思考
查看>>
Celer Network 链下加密经济机制
查看>>
python中#!/usr/bin/python与#!/usr/bin/env python的区别
查看>>
技术解读|马云见证!蚂蚁金服推出全球首个区块链跨境汇款服务
查看>>
第10章:并发和分布式编程 10.1并发性和线程安全性
查看>>
如何更愉快地使用em —— 别说你懂CSS相对单位
查看>>
centos6.x 安装redmine 3.x + apache
查看>>
Python的变量声明
查看>>
多线程之死锁就是这么简单
查看>>
Python字符串格式化
查看>>