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,调了一个小时才发现啊!!!!!我一直以为我的 ...
随机推荐
- 下拉刷新,上拉装载许多其他ListView
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanVuaHVhaG91c2U=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- A Game of Thrones(4) - Eddard
The visitors poured(倾泻:流出) through the castle gates in a river of gold and silver and polished steel ...
- Android横屏竖屏设置
Android横竖屏设置: 方法一:onCreate()中 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // ...
- 理解Git的工作流程(转)
英文原文:Understanding the Git Workflow 如果你不理解Git的设计动机,那你就会处处碰壁.知道足够多的命令和参数后,你就会强行让Git按你想的来工作,而不是按Git自己的 ...
- 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...
- zoj3822 期望dp
每天在一个n*m的棋盘上放棋子,问使得每一行,每一列都有棋子的期望天数 dp[n][m][k] 表示用k个棋子占据了n行,m列,距离目标状态还需要的期望天数 那么dp[n][m][k] = p1 * ...
- uvaLive5713 次小生成树
uvaLive5713 修建道路使得n个点任意两点之间都可以连通,每个点有都有一定的人口,现在可以免费修一条道路, A是免费修的道路两端结点的人口之和, B的其它不是免费修道路的长度的总和 要求的是A ...
- [WPF]入门理解Binding 数据驱动思想
站在一个WinForm程序员的角度去考虑,他会做这样几件事情: 响应slider1的ValueChanged事件,在事件处理函数中让textBox1显示slider1的Value 响应textBox1 ...
- Android开发ListView使用OnScrollListener实现分页加载数据
上篇博文和大家分享了下拉刷新,这是一个用户体验很好的操作方式.新浪微薄就是使用这样的方式的典型. 还有个问题,当用户从网络上读取微薄的时候.假设一下子所有载入用户未读的微薄这将耗费比較长的时间,造成不 ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:3.技术简介之MinaFilter——LoggingFilter (转)
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 LoggingFilter 接下来,使我们对Filter介绍的最后一个——LoggingFilter. 与Proto ...