[BZOJ1834]网络扩容 做题笔记
2016-03-04 11:30
309 查看
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1834
先跑一遍最大流,然后在原残量图的基础上建边。对原有的每条边,新建容量为inf,费用为题目描述的边。新建超级源S,S到1连一条容量为k费用为0的边,在新图上跑一遍费用流。
先跑一遍最大流,然后在原残量图的基础上建边。对原有的每条边,新建容量为inf,费用为题目描述的边。新建超级源S,S到1连一条容量为k费用为0的边,在新图上跑一遍费用流。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N=2009,M=5109,inf=0x3fffffff; int n,m,K; int head ,e[M<<2],ver[M<<2],c[M<<2],next[M<<2]; int ub[M],vb[M],wb[M],cb[M]; int pre ,pos ,d ;bool vis ; int s,t,tot=1; void add (int u,int v,int w,int cost=0) { ver[++tot]=v;e[tot]=w;c[tot]=cost;next[tot]=head[u];head[u]=tot; ver[++tot]=u;e[tot]=0;c[tot]=-cost;next[tot]=head[v];head[v]=tot; } bool spfa () { queue<int> q; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); memset(pos,0,sizeof(pos)); for (int i=0;i<=n+9;i++) d[i]=0x3fffffff; //注意这里,一开始我把n+9错写成了N+9,结果WA了 q.push(s); d[s]=0; vis[s]=1; pre[s]=s; while (!q.empty()) { int x=q.front(); q.pop(); for (int i=head[x];i;i=next[i]) if (e[i]&&d[x]+c[i]<d[ver[i]]) { d[ver[i]]=d[x]+c[i]; pre[ver[i]]=x,pos[ver[i]]=i; if (!vis[ver[i]]) q.push(ver[i]),vis[ver[i]]=1; } vis[x]=0; } return pre[t]!=-1; } int mcf (bool opt) { int cost=0,maxflow=0,aug; while (spfa()) { aug=0x3fffffff; for (int i=t;i!=s;i=pre[i]) aug=min(aug,e[pos[i]]); maxflow+=aug,cost+=aug*d[t]; for (int i=t;i!=s;i=pre[i]) e[pos[i]]-=aug,e[pos[i]^1]+=aug; } if (opt) return cost; return maxflow; } int main () { scanf("%d%d%d",&n,&m,&K); for (int i=1;i<=m;i++) { scanf("%d%d%d%d",&ub[i],&vb[i],&wb[i],&cb[i]); add(ub[i],vb[i],wb[i]); } s=1,t=n; printf("%d ",mcf(0)); s=0,t=n; add(s,1,K,0); for (int i=1;i<=m;i++) add(ub[i],vb[i],inf,cb[i]); printf("%d\n",mcf(1)); }
相关文章推荐
- iOS开发之网络检测
- http状态码的含义
- HTTP权威指南 第四章 连接管理
- VMware虚拟机网络详解
- TCP协议中的三次握手和四次挥手
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片比较
- Java https服务器证书认证问题解决方案
- TCP序列号和确认号详解
- IOS 快速将网络数据转化为所需的对象
- android tcp server
- iOS9 HTTP 不能正常使用的解决办法
- Distribution download cancelled. Using distribution from 'https://services.gradle.org/distributions/
- IP头、TCP头、UDP头详解以及定义
- java实现http多线程下载,断点回复
- xcode7.1 网路问题http/https NSAppTransportSecurity
- HttpWebRequest外部服务
- java网络编程五:暴力法破解登录系统的完全实现(木子)
- java网络编程四:InetAddress类、URL类、URLConnection类解析及用法示例
- java网络编程三:基于SMTP的邮件发送客户端
- java网络编程一:Socket用法,简单模拟一对一聊天