【BZOJ】1834 [ZJOI2010]network 网络扩容
【算法】网络流-最大流+最小费用最大流(费用流)
【题解】
第一问跑最大流。
第二问:
原始边相当于费用为0的边,再原图(跑过最大流的图)基础上添加带费用的边,容量为k(相当于inf)。
第一问最大流使用了哪条边对第二问没有影响,因为费用流肯定优先往费用为0的边(原始边)跑。
限流k?添加超级源向1连容量k费用0的边即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=,maxm=,N=,inf=0x3f3f3f3f;
int tot=,first[maxn],d[maxn],q[maxn],cur[maxn],S,T,sum,n,m,K,u[maxm],v[maxm],w[maxm];
bool vis[maxn];
struct edge{int v,f,c,from;}e[maxm*]; void insert(int u,int v,int f,int c){
tot++;e[tot].v=v;e[tot].f=f;e[tot].c=c;e[tot].from=first[u];first[u]=tot;
tot++;e[tot].v=u;e[tot].f=;e[tot].c=-c;e[tot].from=first[v];first[v]=tot;
}
bool bfs(){
memset(d,-,sizeof(d));
int head=,tail=;q[head]=S;d[S]=;
while(head<tail){
int x=q[head++];
for(int i=first[x];i;i=e[i].from)
if(e[i].f&&d[e[i].v]==-){
d[e[i].v]=d[x]+;
q[tail++]=e[i].v;
}
}
return ~d[T];
}
int dinic(int x,int a){
if(x==T||a==)return a;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(d[e[i].v]==d[x]+&&(f=dinic(e[i].v,min(e[i].f,a)))){
e[i].f-=f;e[i^].f+=f;
flow+=f;a-=f;
if(a==)break;
}
return flow;
}
int solve1(){
int ans=;
while(bfs()){
for(int i=S;i<=T;i++)cur[i]=first[i];
ans+=dinic(S,inf);
}
return ans;
}
bool spfa(){
memset(d,0x3f,sizeof(d));
int head=,tail=;q[head]=T;d[T]=;vis[T]=;
while(head!=tail){
int x=q[head++];if(head>N)head=;
for(int i=first[x];i;i=e[i].from)
if(e[i^].f&&d[x]+e[i^].c<d[e[i].v]){
d[e[i].v]=d[x]+e[i^].c;
if(!vis[e[i].v]){
if(d[e[i].v]<d[q[head]]){if(--head<)head=N;q[head]=e[i].v;}
else{q[tail++]=e[i].v;if(tail>N)tail=;}
vis[e[i].v]=;
}
}
vis[x]=;//
}
return d[S]<inf;
}
int dfs(int x,int a){
if(x==T||a==)return a;
vis[x]=;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(!vis[e[i].v]&&d[e[i].v]+e[i].c==d[x]&&(f=dfs(e[i].v,min(e[i].f,a)))){
e[i].f-=f;e[i^].f+=f;
sum+=e[i].c*f;
flow+=f;a-=f;
if(a==)break;
}
vis[x]=;
return flow;
}
int solve2(){
sum=;
while(spfa()){
for(int i=S;i<=T;i++)cur[i]=first[i];
dfs(S,inf);
}
return sum;
}
int main(){
scanf("%d%d%d",&n,&m,&K);
S=;T=n;
for(int i=;i<=m;i++){
int c;
scanf("%d%d%d%d",&u[i],&v[i],&c,&w[i]);
insert(u[i],v[i],c,);
}
printf("%d ",solve1());
S=;
for(int i=;i<=m;i++)insert(u[i],v[i],K,w[i]);
insert(S,,K,);
printf("%d",solve2());
return ;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=,maxm=,N=,inf=0x3f3f3f3f;
int tot=,first[maxn],d[maxn],q[maxn],cur[maxn],S,T,sum,n,m,K,u[maxm],v[maxm],w[maxm],c[maxm];
bool vis[maxn];
struct edge{int v,f,c,from;}e[maxm*]; void insert(int u,int v,int f,int c){
tot++;e[tot].v=v;e[tot].f=f;e[tot].c=c;e[tot].from=first[u];first[u]=tot;
tot++;e[tot].v=u;e[tot].f=;e[tot].c=-c;e[tot].from=first[v];first[v]=tot;
}
bool bfs(){
memset(d,-,sizeof(d));
int head=,tail=;q[head]=S;d[S]=;
while(head<tail){
int x=q[head++];
for(int i=first[x];i;i=e[i].from)
if(e[i].f&&d[e[i].v]==-){
d[e[i].v]=d[x]+;
q[tail++]=e[i].v;
}
}
return ~d[T];
}
int dinic(int x,int a){
if(x==T||a==)return a;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(d[e[i].v]==d[x]+&&(f=dinic(e[i].v,min(e[i].f,a)))){
e[i].f-=f;e[i^].f+=f;
flow+=f;a-=f;
if(a==)break;
}
return flow;
}
int solve1(){
int ans=;
while(bfs()){
for(int i=S;i<=T;i++)cur[i]=first[i];
ans+=dinic(S,inf);
}
return ans;
}
bool spfa(){
memset(d,0x3f,sizeof(d));
int head=,tail=;q[head]=T;d[T]=;vis[T]=;
while(head!=tail){
int x=q[head++];if(head>N)head=;
for(int i=first[x];i;i=e[i].from)
if(e[i^].f&&d[x]+e[i^].c<d[e[i].v]){
d[e[i].v]=d[x]+e[i^].c;
if(!vis[e[i].v]){
if(d[e[i].v]<d[q[head]]){if(--head<)head=N;q[head]=e[i].v;}
else{q[tail++]=e[i].v;if(tail>N)tail=;}
vis[e[i].v]=;
}
}
vis[x]=;//
}
return d[S]<inf;
}
int dfs(int x,int a){
if(x==T||a==)return a;
vis[x]=;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(!vis[e[i].v]&&d[e[i].v]+e[i].c==d[x]&&(f=dfs(e[i].v,min(e[i].f,a)))){
e[i].f-=f;e[i^].f+=f;
sum+=e[i].c*f;
flow+=f;a-=f;
if(a==)break;
}
vis[x]=;
return flow;
}
int solve2(){
sum=;
while(spfa()){
for(int i=S;i<=T;i++)cur[i]=first[i];
dfs(S,inf);
}
return sum;
}
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]);
insert(u[i],v[i],c[i],);
}
int now=;
printf("%d ",now=solve1());
tot=;memset(first,,sizeof(first));//
for(int i=;i<=m;i++){
insert(u[i],v[i],c[i],);
insert(u[i],v[i],K,w[i]);
}
S=;
insert(S,,now+K,);
printf("%d",solve2());
return ;
}
【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,调了一个小时才发现啊!!!!!我一直以为我的 ...
随机推荐
- MySQL 忘记root密码怎么办
前言:记住如果忘记root密码,在启动MySQL的时候,跳过查询授权表就ok了. 对于RedHat 6 而言 (1)启动mysqld 进程时,为其使用:--skip-grant-tables --sk ...
- C#控件之Repeater控件使用
歡迎大家來討論,修改,一定虛心接受. 1.為什麼使用Repeater控件? 關於把從數據庫讀取的數據綁定到前台頁面,我們可以使用DataGrid.DataGridView以及Repeater來佈局,三 ...
- (转)ActiveMQ的重连机制
花了一天的时间,终于搞明白了我的疑问. failover://(tcp://localhost:6168)?randomize=false&initialReconnectDelay=100& ...
- p2 形状
形状是物理引擎进行碰撞模拟计算的依据,是刚体最基本的属性. P2中使用Shape类来表示形状,通过刚体的addShape()方法,将形状添加到刚体中之后, 就可以随着刚体的移动.旋转不断更新,并进行碰 ...
- MVC4中control的增删改查
public class TestController : Controller { private LeaveEntities db = new LeaveEntities(); // // GET ...
- 【Python】第一篇:python基础_1
本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...
- ZOJ3513_Human or Pig
这个题太坑爹了,题意也好纠结. 是这样的,给你一个n*m的矩形,中间有n*m个1*1的格子,有不同的跳跃方法.如果当前为human(人类)那么他可以有意识的选择自己下一步跳往何方:如果当前为pig(猪 ...
- ZOJ2686_Cycle Gameu
题目的意思是给你一个多边形,每条边上有一个权值,你开始在第一个点.每次你必须经过一条有权值的边,并且把该边的权值减小到任意一个非负值,到达该边的另外一个点. 谁第一个无法操作就算输. 题意很简单,解法 ...
- Java Machine Learning Tools & Libraries--转载
原文地址:http://www.demnag.com/b/java-machine-learning-tools-libraries-cm570/?ref=dzone This is a list o ...
- Hadoop RPC protocol description--转
原文地址:https://spotify.github.io/snakebite/hadoop_rpc.html Snakebite currently implements the followin ...