【BZOJ 1834】 [ZJOI2010]network 网络扩容
Description
Input
Output
Sample Input
1 2 5 8
2 5 9 9
5 1 6 2
5 1 1 8
1 2 8 7
2 5 4 9
1 2 1 1
1 4 2 1
Sample Output
30%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int N=,inf=;
struct ee{int to,next,f,w;}e[N*];
int S,T,cnt=,n,k,ans1,ans2,timer,m,u[N],v[N],w[N],c[N],mid;
int head[N],dis[N],pre[N],q[N];
bool inq[N],flag=;
void ins(int u,int v,int f,int w){
e[++cnt].to=v,e[cnt].next=head[u],e[cnt].f=f,e[cnt].w=w,head[u]=cnt;
e[++cnt].to=u,e[cnt].next=head[v],e[cnt].f=,e[cnt].w=-w,head[v]=cnt;
}
bool spfa(){
for (int i=;i<=T;i++) dis[i]=inf;
int h=,t=;
q[t]=S;dis[S]=;inq[S]=;
while (h!=t){
int now=q[++h];if(h==) h=;
for (int i=head[now];i;i=e[i].next){
int v=e[i].to;
if (dis[v]>dis[now]+e[i].w&&e[i].f){
dis[v]=dis[now]+e[i].w;
pre[v]=i;
if (!inq[v]){
q[++t]=v;if (t==) t=;
inq[v]=;
}
}
}
inq[now]=;
}
if (dis[T]==inf) return ;
return ;
} void updata(){
int tmp=T,flow=inf;
while (tmp!=S){
int l=pre[tmp],v=e[l].to;
flow=min(flow,e[l].f);
tmp=e[l^].to;
}
tmp=T;
while (tmp!=S){
int l=pre[tmp],v=e[l].to;
e[l].f-=flow;e[l^].f+=flow;
tmp=e[l^].to;
}
mid+=flow;
if(mid>=k) flag=;
ans2+=dis[T]*flow;
} bool bfs(){
for (int i=;i<=T;i++) dis[i]=inf;
int h=,t=,now;
q[]=;dis[]=;
while(h!=t){
now=q[++h];
for (int i=head[now];i;i=e[i].next){
int v=e[i].to;
if (e[i].f&&dis[now]+<dis[v]){
dis[v]=dis[now]+;
if (v==n)return ;
q[++t]=v;
}
}
}
if (dis[n]==inf) return ; return ;
} int dinic(int now,int f){
if (now==n) return f;
int rest=f;
for (int i=head[now];i;i=e[i].next){
int v=e[i].to;
if (e[i].f&&dis[v]==dis[now]+&&rest){
int t=dinic(v,min(rest,e[i].f));
if (!t) dis[v]=;
e[i].f-=t;
e[i^].f+=t;
rest-=t;
//if(t) printf("%d %d %d\n",now,v,e[i].f);
}
}
return f-rest;
} int main(){
scanf("%d%d%d",&n,&m,&k);
S=,T=n+;
for (int i=;i<=m;i++){
scanf("%d%d%d%d",&u[i],&v[i],&c[i],&w[i]);
ins(u[i],v[i],c[i],);
}
ins(S,,k,);ins(n,T,k,);
while(bfs())
ans1+=dinic(,inf);
for (int i=;i<=m;i++)ins(u[i],v[i],inf,w[i]);
while(flag&&spfa())
updata();
printf("%d %d",ans1,ans2);
}
【BZOJ 1834】 [ZJOI2010]network 网络扩容的更多相关文章
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...
- 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 网络扩容
#include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
- BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络
对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...
- 【BZOJ】1834 [ZJOI2010]network 网络扩容
[算法]网络流-最大流+最小费用最大流(费用流) [题解] 第一问跑最大流. 第二问: 原始边相当于费用为0的边,再原图(跑过最大流的图)基础上添加带费用的边,容量为k(相当于inf). 第一问最大流 ...
随机推荐
- Scala中的数组
数组 数组的两种声明方式,建议声明数组时指定类型. 访问数组元素时获取数组下标 数组Array类本身有很多非常方便的方法 变长数组ArrayBuffer,能够动态增加元素,也可以实现与Array的互转 ...
- 用VS2015打开cshtml识图文件的时候会报错 如指定的文件不存在
用vs2015打开cshtml识图文件的时候会报错.百度后得到解决方法如下: 先关闭VS2015, 拷贝:%LocalAppData%\Microsoft\VisualStudio\14.0\Comp ...
- 会话跟踪技术——Session
一.什么是Session Session从用户访问页面开始,到断开与网站连接为止,形成一个会话的生命周期.在会话期间,分配客户唯一的一个SessionID,用来标识当前用户,与其他用户进行区分. Se ...
- ASP.NET 模板引擎 - NVelocity
1,HTML的Form表单数据按Button提交数据以后,由 Action 指定的服务器端处理程序(.ashx)进行处理后 ,再响应的浏览器. 2,我们把 HTML的表单,写到 .ashx 一般处理程 ...
- Jersey(1.19.1) - Use of @Context
Previous sections have introduced the use of @Context. The JAX-RS specification presents all the sta ...
- C# 4 dynamic 动态对象 动态类型转换
public class User { //使用省缺参数,一般不需要再为多态做各种静态重载了 public User( string name = "anonym", string ...
- asp.net输出重写压缩页面文件实例
例子 代码如下 复制代码 using System;using System.Data;using System.Configuration;using System.Web;using Syste ...
- Redis rdb文件CRC64校验算法 Java实现
查看RDB文件结构,发现最后的8字节是CRC64校验算得,从文件头开始直到8字节校验码前的FF结束码(含),经过CRC64校验计算发现,貌似最后的8字节是小端模式实现的. 参考redis的crc64实 ...
- Cocos2d-x中使用音频CocosDenshion引擎介绍与音频文件的预处理
Cocos2d-x提供了一个音频CocosDenshion引擎,CocosDenshion引擎可以独立于Cocos2d-x单独使用,CocosDenshion引擎本质上封装了OpenAL音频处理库.具 ...
- wpf DataGrid 双击获取当前行的控件
<DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top& ...