bzoj1834 [ZJOI2010]network 网络扩容
第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可。
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<set>
#include<queue>
#define mp make_pair
#define inf 0x37373737
#define N 30050
#define M 30050
using namespace std;
struct Dinic {
int s, t, n, pre[N], cur[N], h[N], level[N], sign, q[N];
int cap[M], to[M], ne[M], flow, e;
void liu(int u, int v, int w) {
to[e] = v, ne[e] = h[u], cap[e] = w;
h[u] = e++;
}
void link(int u, int v, int w) {
liu(u, v, w);
liu(v, u, );
}
void init(int n) {
for (int i = ; i <= n; ++i)
h[i] = -;
e = ;
}
bool bfs() {
int L = , R = ;
fill(level, level + n, -);
sign = q[R++] = t;
level[t] = ;
while (L < R && level[s] == -) {
int c = q[L++];
for (int k = h[c]; ~k; k = ne[k]) {
if (cap[k ^ ] > && level[to[k]] == -)
level[to[k]] = level[c] + , q[R++] = to[k];
}
}
return ~level[s];
}
void push() {
int pl = inf, p, k;
for (p = t; p != s; p = to[k ^ ]) {
k = pre[p];
pl = min(pl, cap[k]);
}
for (p = t; p != s; p = to[k ^ ]) {
k = pre[p];
cap[k] -= pl;
cap[k ^ ] += pl;
if (cap[k] == )
sign = to[k ^ ];
}
flow += pl;
}
void dfs(int c) {
if (c == t)
push();
else {
for (int &k = cur[c]; ~k; k = ne[k])
if (cap[k] > && level[to[k]] + == level[c]) {
pre[to[k]] = k;
dfs(to[k]);
if (level[sign] > level[c])
return;
sign = t;
}
level[c] = -;
}
}
int run(int _s, int _t, int _n) {
s = _s, t = _t, n = _n;
flow = ;
while (bfs()) {
for (int i = ; i < n; ++i)
cur[i] = h[i];
dfs(s);
}
return flow;
}
} mf; struct MCMF{
int h[N] , dis[N] , ing[N] , pre[N] , s , t , n;
int to[M] , ne[M] , cap[M] , cost[M] , e;
void ini(){
fill(h,h+N,-);
e = ;
}
void liu(int u,int v,int c,int w){
to[e] = v , ne[e] = h[u] , cap[e] = c , cost[e] = w;
h[u] = e++;
}
void link(int u,int v,int c,int w){
liu(u,v,c,w);
liu(v,u,,-w);
}
bool spfa(){
queue<int> Q;
fill(ing,ing+n,);
fill(pre,pre+n,-);
fill(dis,dis+n,inf);
ing[s] = true , dis[s] = ;
Q.push(s);
while(!Q.empty()){
int c = Q.front();Q.pop();ing[c] = false;
for(int k=h[c];~k;k=ne[k]){
int v = to[k];
if(cap[k] <= ) continue;
if(dis[c] + cost[k] < dis[v]){
dis[v] = dis[c] + cost[k];
pre[v] = k;
if(!ing[v]) Q.push(v) , ing[v] = true;
}
}
}
return dis[t] != inf;
}
int flow , mincost;
pair<int,int> run(int _s,int _t,int _n){
s = _s , t = _t , n = _n;
flow = mincost = ;
while(spfa()){
int pl = inf , p , k;
for(p=t;p!=s;p=to[k^]){
k = pre[p];
pl = min(pl,cap[k]);
}
for(p=t;p!=s;p=to[k^]){
k = pre[p];
cap[k] -= pl;
cap[k^] += pl;
}
mincost += pl * dis[t];
flow += pl;
}
return mp(flow,mincost);
}
};
MCMF mcmf; int n,m,k,i,a,b,c,d,flow,cost;
int main()
{
scanf("%d%d%d",&n,&m,&k);
mf.init(n+);
mcmf.ini();
for (i=;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
mf.link(a,b,c);
mcmf.link(a,b,c,);
mcmf.link(a,b,,d);
}
flow=mf.run(,n,n+);
mcmf.link(,,flow+k,);
printf("%d %d",flow,mcmf.run(,n,n+).second);
}
bzoj1834 [ZJOI2010]network 网络扩容的更多相关文章
- BZOJ1834 [ZJOI2010]network 网络扩容 【最大流,费用流】
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 3394 Solved: 1774 [Subm ...
- [BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 3330 Solved: 1739 [Subm ...
- bzoj1834: [ZJOI2010]network 网络扩容 费用流
bzoj1834 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容 ...
- 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: ...
- 2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)
传送门 网络流水题啊. 第一问直接放心跑最大流(本来还以为有什么tricktricktrick). 第二问就直接把原来的边(u,v,c,w)(u,v,c,w)(u,v,c,w)变成(u,v,c,0)( ...
- 【最大流】【费用流】bzoj1834 [ZJOI2010]network 网络扩容
引用题解: 最大流+费用流. 第一问最大流即可. 第二问为“最小费用最大流”. 由题意,这一问的可转化为在上一问的“残量网络”上,扩大一些边的容量,使能从新的图中的最大流为k. 那么易得:对于还有剩余 ...
- BZOJ1834 [ZJOI2010]network 网络扩容(最小费用最大流)
挺直白的构图..最小费用最大流的定义. #include<cstdio> #include<cstring> #include<queue> #include< ...
- 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流
[BZOJ1834][ZJOI2010]network 网络扩容 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不 ...
- bzoj1834: [ZJOI2010]network 网络扩容
努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...
随机推荐
- php apc
APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓存. 系统缓存 它是 ...
- js弹出确认框,挺全
一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.google.com'">弹 ...
- PHP实现QQ第三方登录
PHP实现QQ第三方登录 学习之前,请大家先看一下oAuth协议. 首先呢,我们进入QQ互联的官方网站 http://connect.qq.com登入我们自己的QQ号,没有QQ号的小伙伴可以忽略本篇博 ...
- NDK编译FreeImage
参考了 以下2篇文章 并作了一小点修改 http://recursify.com/blog/2013/05/25/building-freeimage-for-android http://blog. ...
- windows下安装和配置mongoDB
上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...
- memory_limit的一个bug | 风雪之隅
原文:memory_limit的一个bug | 风雪之隅 27 Nov 09 memory_limit的一个bug 作者: Laruence( ) 本文地址: http://www.laruence. ...
- Top 30 Nmap Command Examples For Sys/Network Admins
Nmap is short for Network Mapper. It is an open source security tool for network exploration, securi ...
- (lleetcode)Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- 针对应用程序池“xxxxxx”的模板永久性缓存初始化失败,解决方法
日志名称: Application 来源: Active Server Pages 日期: 2014-11-22 9:09:39 事件 I ...
- iOS: 实现苹果的内购
一.介绍: 在个人开发的app上架到AppStore后,苹果官方允许我们将自己的app在appstore上进行付费使用,也就是所谓的内购.其中,支付方式规定的必须是苹果的支付方式:应用内支付. 二.流 ...