1362 网络扩容

省队选拔赛

 时间限制: 2 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 
题目描述 Description

给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求:

1、  在不扩容的情况下,1到N的最大流;

2、  将1到N的最大流增加K所需的最小扩容费用。

输入描述 Input Description

输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。

接下来的M行每行包含四个整数u,v,C,W,表示一条从uv,容量为C,扩容费用为W的边。

输出描述 Output Description

输出文件一行包含两个整数,分别表示问题1和问题2的答案。

样例输入 Sample Input

5 8 2

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

13 19

数据范围及提示 Data Size & Hint

30%的数据中,N<=100

100%的数据中,N<=1000,M<=5000,K<=10

题解:

在最大流的参与网络上加新边跑最小费用最大流!

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxn 10010
#define inf 0x7fffffff
#define S 1
#define T n using namespace std; int n,m,K,cnt=,vw[maxn],ans1,ans2,U[maxn],V[maxn],head[maxn],x,dis[maxn],inq[maxn],from[maxn]; struct ss
{
int to;
int next;
int c;
int w;
int from;
}e[]; void add(int u,int v,int c,int w)
{
e[++cnt].to=v;
e[cnt].next=head[u];
e[cnt].from=u;
e[cnt].c=c;
e[cnt].w=w;
head[u]=cnt;
} void insert(int u,int v,int c,int w)
{
add(u,v,c,w);
add(v,u,,-w);
} bool bfs()
{
for (int i=;i<=T;i++) dis[i]=inf;
queue<int> que;
que.push(S);
dis[S]=;
while (!que.empty())
{
int now=que.front();
que.pop();
for (int i=head[now];i;i=e[i].next)
if (e[i].c&&dis[e[i].to]>dis[now]+)
{
dis[e[i].to]=dis[now]+;
que.push(e[i].to);
if (e[i].to==T) return ;
}
}
return ;
} int dinic(int x,int INF)
{
if (x==T) return INF;
int rest=INF;
for (int i=head[x];i;i=e[i].next)
if (e[i].c&&dis[e[i].to]==dis[x]+)
{
int now=dinic(e[i].to,min(rest,e[i].c));
e[i].c-=now;
if (!now) dis[now]=;
e[i^].c+=now;
rest-=now;
}
return INF-rest;
} bool spfa()
{
for (int i=;i<=T;i++) dis[i]=inf;
queue<int> que;
que.push();
inq[]=;
dis[]=;
while (!que.empty())
{
int now=que.front();
que.pop();
inq[now]=;
for (int i=head[now];i;i=e[i].next)
if (e[i].c&&dis[e[i].to]>dis[now]+e[i].w)
{
dis[e[i].to]=dis[now]+e[i].w;
from[e[i].to]=i;
if (!inq[e[i].to])
{
inq[e[i].to]=;
que.push(e[i].to);
}
}
}
if (dis[T]==inf) return ;
else return ;
} int bcf()
{
int x=inf;
for (int i=from[T];i;i=from[e[i].from])
x=min(x,e[i].c);
for (int i=from[T];i;i=from[e[i].from])
{
ans2+=x*e[i].w;
e[i].c-=x;
e[i^].c+=x;
}
} int main()
{
scanf("%d%d%d",&n,&m,&K);
for (int i=;i<=m;i++)
{
int u,v,w,c;
scanf("%d%d%d%d",&U[i],&V[i],&c,&vw[i]);
insert(U[i],V[i],c,);
}
while (bfs())
while (x=dinic(S,inf)) ans1+=x;
insert(,,K,);
printf("%d ",ans1);
for (int i=;i<=m;i++)
insert(U[i],V[i],K,vw[i]);
while(spfa()) bcf();
printf("%d\n",ans2);
return ;
}

C++之路进阶——codevs1362(网络扩容)的更多相关文章

  1. BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2269 Solved: 1136 [Submit ...

  2. 【BZOJ】【1834】【ZJOI2010】Network 网络扩容

    网络流/费用流 这题……我一开始sb了. 第一问简单的最大流…… 第二问是要建费用流的图的……但是是在第一问的最大流跑完以后的残量网络上建,而不是重建…… 我们令残量网络上原有的弧的费用全部为0(因为 ...

  3. bzoj1834: [ZJOI2010]network 网络扩容

    努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...

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

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

  5. App 组件化/模块化之路——如何封装网络请求框架

    App 组件化/模块化之路——如何封装网络请求框架 在 App 开发中网络请求是每个开发者必备的开发库,也出现了许多优秀开源的网络请求库.例如 okhttp retrofit android-asyn ...

  6. 【BZOJ1834】网络扩容(最大流,费用流)

    [BZOJ1834]网络扩容(最大流,费用流) 题面 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下 ...

  7. BZOJ_1834_[ZJOI2010]network 网络扩容_费用流

    BZOJ_1834_[ZJOI2010]network 网络扩容_费用流 题意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求:  1.在不扩容的 ...

  8. 【题解】Luogu P2604 [ZJOI2010]网络扩容

    原题传送门:P2604 [ZJOI2010]网络扩容 这题可以说是板题 给你一个图,先让你求最大流 再告诉你,每条边可以花费一些代价,使得流量加一 问至少花费多少代价才能使最大流达到k 解法十分简单 ...

  9. 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流

    [BZOJ1834][ZJOI2010]network 网络扩容 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不 ...

随机推荐

  1. shell判断文件或者文件夹是否存在

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  2. VS2010 F5调试时出现:“ 尝试运行项目时出错:未捕获通过反射调用的方法引发的异常”解决

    VS2010 F5调试时出现 尝试运行项目时出错:未捕获通过反射调用的方法引发的异常 两个解决方法:1) 打开项目属性,选择调试选项卡,将“启用非托管代码调试”一项钩上.2) 打开项目属性,选择调试选 ...

  3. 【转】在C#中使用SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

  4. About_MySQL Select--来自copy_03

    MySQL Select   查询分类 单表查询:简单查询 多表查询:连接查询 联合查询:多个查询结果汇总 查询的组成 投影查询:挑选要显示的字段 select array1,array2,... f ...

  5. QSpinBox 和 QSlider 联合使用方法

    在Qt中,有时候我们想要联合QSpinBox 和 QSlider,使得移动滑块,QSpinBox中的数据会变化,或者我们在QSpinBox中输入一个数值,响应的滑块也会变化,如下图所示:

  6. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  7. ado.net 修改,查询

    修改: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  8. Linux_文件及文件夹[创建][复制][移动][删除][重命名]

    一.文件/文件夹创建 1.文件的创建 touch , vi/vim/nano , ... 语   法: touch [-acfm][-d <日期时间>][-r <参考文件或目 录&g ...

  9. 关于padding与margin的区别

    代码一:全为padding. <!doctype html><html><head>    <meta charset="UTF-8"&g ...

  10. 图片懒加载插件lazyload使用方法

    图片懒加载插件lazyload使用方法 一.如何使用: Lazy Load 依赖于 jQuery.引入文件 <script type="text/javascript" sr ...