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,调了一个小时才发现啊!!!!!我一直以为我的 ...
随机推荐
- 关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我
来源: http://zhidao.baidu.com/link?url=qxzkx5gaoCfnHnygYdzaLEWkC45JqNYYUk42eHHjB0yB3ZMgHv6lGjnq3CRfgQw ...
- Http协议学习总结(转)
因为项目中很多地方都与Http协议有关,零散的了解了一下Http协议,但是没有系统的学习过. 今天根据网上其他同学的整理,加上我的一些经验,我也整理了一份.当做学习记录吧. 一.什么是HTTP协议 H ...
- Vagrant - 百度百科
http://wapbaike.baidu.com/view/9201587.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...
- Python使用时间戳
1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 importtime timeArray = time.strpti ...
- SqlServer操作远程数据库
exec sp_addlinkedserver 'srv2','','mssql2008','服务器IP' exec sp_addlinkedsrvlogin 'srv2','false',null, ...
- SE 2014年4月1日
一. 描述OSPF报文都有哪些,其作用? OSPF报文主要有:hello报文.DD报文.LSR报文.LSU报文和LSAck报文. Hello报文主要用来建立和维护邻居关系. DD报文是链路状态数据库的 ...
- Linux下SVN安装配置全程实录(转)
一.安装SVN默认安装到/usr/local/bin下面 二.创建仓库 svnadmin create /home/svnrepo /root/svnrepo为所创建仓库的路径,理论上可以是任何目录 ...
- 登录oracle时,scott is locked (帐户被锁定) 的解决方法
登录Oracle时,用scott/tiger 通常此时会报一个错误: scott is locked (帐户被锁定) 现在就要用超级用户system将scott帐户进行解锁. cmd->sql ...
- tomcat启动Flash退出错误不能被视为解决该错误信息
tomcat 当有错误 启动startup.bat闪存在退出解决方案 打开 startup.bat 文件 最后 该start 阅读run watermark/2/text/aHR0cDovL2Jsb2 ...
- vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式
vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式 #Region "WPF 当浏览器窗体关闭 ...