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

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

注意: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. leetcode814 Binary Tree Pruning

    """ We are given the head node root of a binary tree, where additionally every node's ...

  2. Lesson 7 Bats

    In what way does echo-location in bats play a utilitarian role? Not all sounds made by animals serve ...

  3. Springboot配置文件内容加密

      使用的是jasypt-spring-boot-starter,具体介绍可以参考 https://gitee.com/yangziyi2017/Jasypt-Spring-Boot   引入依赖 & ...

  4. Vue 中使用Ajax请求

    Vue 项目中常用的 2 个 ajax 库 (一)vue-resource vue 插件, 非官方库,vue1.x 使用广泛 vue-resource 的使用 在线文档   https://githu ...

  5. JuJu团队12月29号工作汇报

    JuJu团队12月29号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 飞飞 数据处理 待安排 无 婷婷 调试代码 提升acc 无 恩升 修正evaluate 待完成 无 金华 ...

  6. MQTT 协议学习:001-搭建MQTT通信环境,并抓包测试

    背景 目的:了解MQTT 通信的有关概念与流程:方便推算某些数据与文档描述是否一致. 为了能够在保证学习质量的前提下,降低配置环境的门槛,我们将服务器搭建在windwos中,实行内网间的MQTT协议访 ...

  7. 使用PYaudio录制音频和视频(自己)

    参考:https://blog.csdn.net/zhaoyun_zzz/article/details/84341801 音频录制:简洁版 import pyaudioimport waveimpo ...

  8. 我的第一个爬虫【python selenium】

    去年写的一个小功能,一年过得好快,好快! 目的:爬取京东商品详情页面的内容(商品名称.价格.评价数量)后存储到xls文档中,方便商家分析自己商品的动态. 软件:chrome(windows).chro ...

  9. jenkins#安装gitlab

    通过docker安装gitlab 参考 ------------------------------ 拉docker 镜像: docker pull gitlab/gitlab-ce 创建目录存储gi ...

  10. java理解抽象类 2.19

    // Telphone.java public abstract class Telphone{ public abstract void call(); public abstract void m ...