BZOJ 1834 ZJOI2010 network 网络扩展 Dinic+EK费用流
标题效果:给定一个n积分m无向图边,每一方有一个扩展的成本c。代表扩张1费用的交通,寻求最大流量和扩大的最大流量k最小成本
第一问直接运行的最大流量
第二个问题将是连接到一个流的末端每个边缘的起点是正无穷大、费用c缘 然后,n汇点被连接到流动ans+k 费用为0的边 跑最小费用最大流就可以
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 5010
#define INF 0x3f3f3f3f
#define S 1
#define T (n+1)
using namespace std;
struct edge{
int x,y,f,c;
}edges[M];
struct abcd{
int to,f,c,next;
}table[M<<2];
int head[M],tot=1;
int n,m,k,ans,anscost;
int dpt[M];
void Add(int x,int y,int f,int c)
{
table[++tot].to=y;
table[tot].f=f;
table[tot].c=c;
table[tot].next=head[x];
head[x]=tot;
}
bool BFS()
{
static int q[M],r,h;
int i;
memset(dpt,-1,sizeof dpt);
r=h=0;q[++r]=S;dpt[S]=1;
while(r!=h)
{
int x=q[++h];
for(i=head[x];i;i=table[i].next)
if(table[i].f&&!~dpt[table[i].to])
{
dpt[table[i].to]=dpt[x]+1;
q[++r]=table[i].to;
if(table[i].to==T)
return true;
}
}
return false;
}
int Dinic(int x,int flow)
{
int i,left=flow;
if(x==T) return flow;
for(i=head[x];i&&left;i=table[i].next)
if(table[i].f&&dpt[table[i].to]==dpt[x]+1)
{
int temp=Dinic(table[i].to,min(left,table[i].f) );
if(!temp) dpt[table[i].to]=-1;
left-=temp;
table[i].f-=temp;
table[i^1].f+=temp;
}
return flow-left;
}
bool EK()
{
static int q[65540],flow[M],cost[M],from[M];
static bool v[M];
static unsigned short r,h;
int i;
memset(cost,0x3f,sizeof cost);
cost[S]=0;flow[S]=INF;q[++r]=S;
while(r!=h)
{
int x=q[++h];v[x]=0;
for(i=head[x];i;i=table[i].next)
if(table[i].f)
if(cost[table[i].to]>cost[x]+table[i].c)
{
cost[table[i].to]=cost[x]+table[i].c;
flow[table[i].to]=min(flow[x],table[i].f);
from[table[i].to]=i;
if(!v[table[i].to])
v[table[i].to]=1,q[++r]=table[i].to;
}
}
if(cost[T]==INF) return false;
anscost+=flow[T]*cost[T];
for(i=from[T];i;i=from[table[i^1].to])
table[i].f-=flow[T],table[i^1].f+=flow[T];
return true;
}
int main()
{
int i;
cin>>n>>m>>k;
for(i=1;i<=m;i++)
{
scanf("%d%d",&edges[i].x,&edges[i].y);
scanf("%d%d",&edges[i].f,&edges[i].c);
Add(edges[i].x,edges[i].y,edges[i].f,0);
Add(edges[i].y,edges[i].x,0,0);
}
Add(n,T,INF,0);
Add(T,n,0,0);
while( BFS() )
ans+=Dinic(S,INF);
table[tot-1].f=k;
for(i=1;i<=m;i++)
{
Add(edges[i].x,edges[i].y,INF,edges[i].c);
Add(edges[i].y,edges[i].x,0,-edges[i].c);
}
while( EK() );
cout<<ans<<' '<<anscost<<endl;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
BZOJ 1834 ZJOI2010 network 网络扩展 Dinic+EK费用流的更多相关文章
- BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...
- BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络
对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...
- bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- bzoj 1834: [ZJOI2010]network 网络扩容
#include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...
- bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...
- BZOJ 1834 [ZJOI2010]network 网络扩容(费用流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题目大意] 给定一张有向图,每条边都有一个容量C和一个扩容费用W. 这里扩容费 ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...
随机推荐
- Http协议学习总结(转)
因为项目中很多地方都与Http协议有关,零散的了解了一下Http协议,但是没有系统的学习过. 今天根据网上其他同学的整理,加上我的一些经验,我也整理了一份.当做学习记录吧. 一.什么是HTTP协议 H ...
- poj 2769 Reduced ID Numbers(memset使用技巧)
Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...
- ubuntu12 下怎样上网
1,host 就是WIN7 使用WIFI上网 2.打开设置你的VM 8 edit--Virutal network editor--VMnet0--Bridged (connect VMs to di ...
- 单片机实验: 三轴磁场模块 GY-271
最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...
- 服务器编程入门(11)TCP并发回射服务器实现 - 单线程select实现
问题聚焦: 当客户端阻塞于从标准输入接收数据时,将读取不到别的途径发过来的必要信息,如TCP发过来的FIN标志. 因此,进程需要内核一旦发现进程指定的一个或多个IO条件就绪(即输入已准备好被读取,或者 ...
- 为智能硬件提供一站式解决方案——机智云GoKit评测
为智能硬件提供一站式解决方案——机智云GoKit评测 2014年12月24日 作者: ladouyu 3 17,414+ 4 EMW3162GoKit开发板STM32F103智能硬件机智云2.0 从物 ...
- Luci - UCI (Unified Configuration Interface)
参考: http://wiki.openwrt.org/doc/techref/uc http://luci.subsignal.org/api/luci/modules/luci.model.uci ...
- 怎样在屏幕上显示多个alv
本文解说怎样在屏幕上显示多个alv. 实现这种需求关键是下面几点(举例:在屏幕上显示4个alv): 1.须要定义4个alv control 2.由于有4个alv control,于是就须要定义4个容器 ...
- 关于SSIS批量抽取Excel文件报0x80004005错误的解决办法
原文:关于SSIS批量抽取Excel文件报0x80004005错误的解决办法 标题: Microsoft Visual Studio ------------------------------ Pa ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...