题解:先在原网络上跑最大流,然后加上带费用的边跑费用流

高一的时候做这道题怎么想不到?

注意:maxn代表的不一定是同一个变量的范围

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int inf=1000000000;
const int maxn=5009; int n,m,k; struct Edge{
int from,to,cap,flow,cost;
};
vector<int>G[maxn];
vector<Edge>edges;
void Addedge(int x,int y,int z,int w){
Edge e;
e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w;
edges.push_back(e);
e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w;
edges.push_back(e);
int c=edges.size();
G[x].push_back(c-2);
G[y].push_back(c-1);
} int s,t,totn;
queue<int>q;
int inq[maxn];
int d[maxn];
int p[maxn];
int Spfa(int &nowflow,int &nowcost){
for(int i=1;i<=totn;++i){
d[i]=inf;inq[i]=0;
}
q.push(s);inq[s]=1;d[s]=0;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
for(int i=0;i<G[x].size();++i){
Edge e=edges[G[x][i]];
if((e.cap>e.flow)&&(d[x]+e.cost<d[e.to])){
d[e.to]=d[x]+e.cost;
p[e.to]=G[x][i];
if(!inq[e.to]){
inq[e.to]=1;
q.push(e.to);
}
}
}
} if(d[t]==inf)return 0; int x=t,a=inf;
while(x!=s){
Edge e=edges[p[x]];
a=min(a,e.cap-e.flow);
x=e.from;
}
nowflow+=a;nowcost+=a*d[t];
x=t;
while(x!=s){
edges[p[x]].flow+=a;
edges[p[x]^1].flow-=a;
x=edges[p[x]].from;
}
return 1;
} int rx[maxn],ry[maxn],rf[maxn],rw[maxn];
int ans,ans2; void Minit(){
ans=ans2=0;
for(int i=1;i<=n+1;++i)G[i].clear();
edges.clear();
while(!q.empty())q.pop();
} int main(){
scanf("%d%d%d",&n,&m,&k);
Minit();
for(int i=1;i<=m;++i){
scanf("%d%d%d%d",&rx[i],&ry[i],&rf[i],&rw[i]);
Addedge(rx[i],ry[i],rf[i],0);
} totn=n;s=1;t=n;
while(Spfa(ans,ans2));
printf("%d ",ans); Addedge(n+1,1,k,0);
totn=n+1;s=n+1;
for(int i=1;i<=m;++i){
Addedge(rx[i],ry[i],inf,rw[i]);
}
while(Spfa(ans,ans2));
printf("%d\n",ans2);
return 0;
}

  

1834 [ZJOI2010]network 网络扩容的更多相关文章

  1. BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)

    第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然  后跑最小费用最大流就OK了. ---- ...

  2. bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec  Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...

  3. 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...

  4. bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...

  5. BZOJ 1834 [ZJOI2010]network 网络扩容(费用流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题目大意] 给定一张有向图,每条边都有一个容量C和一个扩容费用W. 这里扩容费 ...

  6. bzoj 1834: [ZJOI2010]network 网络扩容

    #include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...

  7. bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】

    第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...

  8. BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络

    对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...

  9. BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)

    一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...

随机推荐

  1. 【LOJ2540】「PKUWC2018」随机算法

    题意 题面 给一个 \(n\) 个点 \(m\) 条边的无向图.考虑如下求独立集的随机算法:随机一个排列并按顺序加点.如果当前点能加入独立集就加入,否则不加入.求该算法能求出最大独立集的概率. \(n ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:将图片变为圆形 (IE8 不支持)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Python3 中 的 绝对导入 与 相对导入

    背景: 在学习tf的时候,看到了from __future__ import absolute_import,所以登记学习一下. 概览: 一般模块导入规则: import xxx时搜索文件的优先级如下 ...

  4. Deep Image Retrieval: Learning global representations for image search In ECCV, 2016学习笔记

    - 论文地址:https://arxiv.org/abs/1604.01325 contribution is twofold: (i) we leverage a ranking framework ...

  5. python merge、join、concat用法与区别

     由于合并变化较大,以后函数可能会修改,只给出一些例子作为参考 总结: merge.join 1.当没有索引时:merge.join为按照一定条件合并 2.当有索引.并按照索引合并时,得到结果为两者混 ...

  6. jsp中include的两种用法

    JSP中的include的两种用法 1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用 ...

  7. Tasks、 activity 及 activity stack - 人间奇迹(转)

      http://www.cnblogs.com/yaozhongxiao/p/3365345.html Activity之间的跳转,或者说加载一个新的Activity,一般对于开发者来说,都不是一个 ...

  8. [Codeforces]1263B PIN Code

    题目 A PIN code is a string that consists of exactly 444 digits. Examples of possible PIN codes: 70137 ...

  9. 06.Delphi接口的不对等的多重继承

    uSayHello代码如下 unit uSayHello; interface uses SysUtils, Windows, Messages, Classes, Graphics, Control ...

  10. 使用Indy解决Could not load SSL Library错误

    测试平台:DelphiXE7 + Indy 10.6.0.5169 + Win7 64bit 步骤: 1. SSL下载版本:openssl-1.0.1j-i386-win32 可去http://yun ...