洛谷 P2604 [ZJOI2010]网络扩容
题目描述
给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、 在不扩容的情况下,1到N的最大流; 2、 将1到N的最大流增加K所需的最小扩容费用。
输入输出格式
输入格式:
输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。 接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。
输出格式:
输出文件一行包含两个整数,分别表示问题1和问题2的答案。
输入输出样例
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
13 19
说明
30%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
第一问 任何费用为0,流量为给定流量的最大流
第二问 源点到第一个点流量为k 其他流量为inf ,费用为给定费用的费用流
#include <cstring>
#include <ctype.h>
#include <cstdio>
#include <queue>
#define M 50005
#define inf 0x7fffffff using namespace std;
void read(int &x)
{
x=;bool f=;
register char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
x=f?(~x)+:x;
}
struct Edge
{
int next,to,flow,value;
Edge(int next=,int to=,int flow=,int value=) :next(next),to(to),flow(flow),value(value) {}
}edge[M<<];
bool vis[M<<];
int u[M],v[M],c[M],w[M],n,m,k,dep[M<<],dis[M<<],fa[M<<],flow[M<<],head[M<<],cnt=;
void insert(int u,int v,int w,int l)
{
edge[++cnt]=Edge(head[u],v,w,l);
head[u]=cnt;
}
bool bfs(int s,int t)
{
memset(dep,-,sizeof(dep));
dep[s]=;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]==-&&edge[i].flow>)
{
dep[v]=dep[now]+;
if(v==t) return true;
Q.push(v);
}
}
}
return false;
}
int min(int a,int b) {return a>b?b:a;}
int dfs(int now,int t,int came_flow)
{
if(now==t||came_flow==) return came_flow;
int f,res=;
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]==dep[now]+&&edge[i].flow>&&(f=dfs(v,t,min(came_flow,edge[i].flow))))
{
res+=f;
came_flow-=f;
edge[i].flow-=f;
edge[i^].flow+=f;
if(came_flow==) break;
}
}
if(res!=came_flow) dep[now]=-;
return res;
}
bool spfa(int s,int t)
{
for(int i=s;i<=t;i++) {flow[i]=inf;dis[i]=inf;vis[i]=;}
vis[s]=;
fa[s]=;
dis[s]=;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
vis[now]=;
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[now]+edge[i].value&&edge[i].flow>)
{
dis[v]=dis[now]+edge[i].value;
flow[v]=min(flow[now],edge[i].flow);
fa[v]=i;
if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
return dis[t]<inf;
}
int update(int s,int t)
{
int x=flow[t];
for(int i=t;i!=s&&i;i=edge[fa[i]^].to)
{
edge[fa[i]].flow-=x;
edge[fa[i]^].flow+=x;
}
return dis[t]*x;
}
int dinic(int s,int t,int type)
{
int ans=;
if(type==)
for(;bfs(s,t);ans+=dfs(s,t,inf));
else for(;spfa(s,t);ans+=update(s,t));
return ans;
}
int main()
{
read(n);
read(m);
read(k);
for(int i=;i<=m;i++)
{
read(u[i]);
read(v[i]);
read(c[i]);
read(w[i]);
insert(u[i],v[i],c[i],);
insert(v[i],u[i],,);
}
printf("%d ",dinic(,n,));
for(int i=;i<=m;i++)
{
insert(u[i],v[i],inf,w[i]);
insert(v[i],u[i],,-w[i]);
}
insert(,,k,);
insert(,,,);
printf("%d\n",dinic(,n,));
return ;
}
洛谷 P2604 [ZJOI2010]网络扩容的更多相关文章
- 洛谷 P2604 [ZJOI2010]网络扩容 解题报告
P2604 [ZJOI2010]网络扩容 题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. ...
- 洛谷$P2604\ [ZJOI2010]$网络扩容 网络流
正解:网络流 解题报告: 传送门$QwQ$ 昂第一问跑个最大流就成不说$QwQ$ 然后第二问,首先原来剩下的边就成了费用为0的边?然后原来的所有边连接的两点都给加上流量为$inf$费用为$w$的边,保 ...
- [洛谷P2604][ZJOI2010]网络扩容
题目大意:给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小费用. 题解 ...
- 【题解】Luogu P2604 [ZJOI2010]网络扩容
原题传送门:P2604 [ZJOI2010]网络扩容 这题可以说是板题 给你一个图,先让你求最大流 再告诉你,每条边可以花费一些代价,使得流量加一 问至少花费多少代价才能使最大流达到k 解法十分简单 ...
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- P2604 [ZJOI2010]网络扩容
思路 简单的费用流问题,跑出第一问后在残量网络上加边求最小费用即可 代码 #include <cstdio> #include <algorithm> #include < ...
- 洛谷 P1546 最短网络 Agri-Net
题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...
- [Luogu 2604] ZJOI2010 网络扩容
[Luogu 2604] ZJOI2010 网络扩容 第一问直接最大流. 第二问,添加一遍带费用的边,边权 INF,超级源点连源点一条容量为 \(k\) 的边来限流,跑费用流. 大约是第一次用 nam ...
- 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)
洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...
随机推荐
- DataUtils
package com.cc.hkjc.util; import java.text.ParseException;import java.text.SimpleDateFormat;import j ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
- SpringMVC之使用Validator接口进行验证
对于任何一个应用而言在客户端做的数据有效性验证都不是安全有效的,这时候就要求我们在开发的时候在服务端也对数据的有效性进行验证.SpringMVC自身对数据在服务端的校验有一个比较好的支持,它能将我们提 ...
- python-day9-进程、线程、协程篇
python threading模块 线程有两种调用方式: 直接调用 import threading import time def sayhi(num): #定义每个线程要运行的函数 print( ...
- Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法Windows版(手贱强制重启电脑的后果)
转自:https://blog.csdn.net/rrrrroy_ha/article/details/80601497
- Linux多台服务器间SSH免密码登录配置
SSH实现各个服务器间的文件相互备份,如运行scp命令,可以实现免密码登录,从而可以使用SHELL脚本实现一些自动化的处理. 假如A机要免密码登录B机,具体方法如下: 1.在A机运行:"ss ...
- Mac系统下的php扩展开发
通常在开发PHP的时候,一些核心代码,比如加密函数或需要高效率执行的代码,此时可以用C语言写扩展.本文主要介绍了扩展的开发流程,具体的代码实现参考生成的文件说明. 当前PHP使用的是XAMPP 5.6 ...
- 51nod 1094 【水题】
暴力即可!!! #include <stdio.h> #include <string.h> #include <iostream> using namespace ...
- LuoguP2822 组合数问题(组合数,二维前缀和)
P2822 组合数问题 输入输出样例 输入样例#1: 复制 1 2 3 3 输出样例#1: 复制 1 输入样例#2: 复制 2 5 4 5 6 7 输出样例#2: 复制 0 7 说明 [样例1说明] ...
- 问题 3: 糖果数(candy)
问题 3: 糖果数(candy) 题目描述 学校准备去春游,委托小明分发糖果,每位同学一袋,数量随机. 一共有N袋糖果,编号为1到N,小明拿了从编号a袋到编号b袋的糖果去分发,小明想知道,他一共拿了多 ...