题目链接

第一问就是简单的最大流。

第二问,保留第一问求完最大流的残量网络。

然后新建一个源点,向原源点连一条流量为k,费用为0的边。

然后所有边重新连一起(原来的边保留),费用为题目所给,最小费用即为第二问答案,很好理解。

#include <cstdio>
#include <queue>
#include <cstring>
#define INF 2147483647
using namespace std;
const int MAXN = 1010;
const int MAXM = 20010;
struct Edge{
int from, next, to, rest, cost;
}e[MAXM];
int head[MAXN], num = 1, n, m, k;
inline void Add(int from, int to, int flow, int cost){
e[++num] = (Edge){from, head[from], to, flow, cost}; head[from] = num;
e[++num] = (Edge){to, head[to], from, 0, -cost}; head[to] = num;
}
int s, t, a[MAXM], b[MAXM], c[MAXM], d[MAXM], now, maxflow, mincost;
queue <int> q;
int v[MAXN], dis[MAXN], pre[MAXN], flow[MAXN];
int re(){
q.push(s);
memset(dis, 127, sizeof dis);
memset(flow, 0, sizeof flow);
dis[s] = 0; pre[t] = 0; flow[s] = INF;
while(q.size()){
now = q.front(); q.pop(); v[now] = 0;
for(int i = head[now]; i; i = e[i].next)
if(e[i].rest && dis[e[i].to] > dis[now] + e[i].cost){
dis[e[i].to] = dis[now] + e[i].cost;
pre[e[i].to] = i; flow[e[i].to] = min(flow[now], e[i].rest);
if(!v[e[i].to]) v[e[i].to] = 1, q.push(e[i].to);
}
}
return pre[t];
}
int main(){
scanf("%d%d%d", &n, &m, &k); s = 1; t = n;
for(int i = 1; i <= m; ++i){
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
Add(a[i], b[i], c[i], 0);
}
while(re()){
now = pre[t];
while(now){
e[now].rest -= flow[t];
e[now ^ 1].rest += flow[t];
now = pre[e[now].from];
}
maxflow += flow[t];
}
printf("%d ", maxflow);
s = 0; Add(s, 1, k, 0);
for(int i = 1; i <= m; ++i)
Add(a[i], b[i], INF, d[i]);
while(re()){
now = pre[t];
while(now){
e[now].rest -= flow[t];
e[now ^ 1].rest += flow[t];
mincost += e[now].cost * flow[t];
now = pre[e[now].from];
}
}
printf("%d\n", mincost);
return 0;
}

【洛谷 P2604】 [ZJOI2010]网络扩容(最大流,费用流)的更多相关文章

  1. 洛谷 P2604 [ZJOI2010]网络扩容 解题报告

    P2604 [ZJOI2010]网络扩容 题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. ...

  2. [洛谷P2604][ZJOI2010]网络扩容

    题目大意:给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小费用. 题解 ...

  3. 洛谷$P2604\ [ZJOI2010]$网络扩容 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 昂第一问跑个最大流就成不说$QwQ$ 然后第二问,首先原来剩下的边就成了费用为0的边?然后原来的所有边连接的两点都给加上流量为$inf$费用为$w$的边,保 ...

  4. 洛谷 P2604 [ZJOI2010]网络扩容

    题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的最小扩容费用. ...

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

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

  6. BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)

    题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...

  7. P2604 [ZJOI2010]网络扩容

    思路 简单的费用流问题,跑出第一问后在残量网络上加边求最小费用即可 代码 #include <cstdio> #include <algorithm> #include < ...

  8. 【洛谷 P1251】 餐巾计划问题 (费用流)

    题目链接 我做的网络流24题里的第一题.. 想是不可能想到的,只能看题解. 首先,我们拆点,将一天拆成晚上和早上,每天晚上会受到脏餐巾(来源:当天早上用完的餐巾,在这道题中可理解为从原点获得),每天早 ...

  9. 洛谷 P2045 方格取数加强版【费用流】

        题目链接:https://www.luogu.org/problemnew/show/P2045 题目描述 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现 ...

  10. 洛谷P2604 网络扩容 拆点+费用流

    原题链接 这题貌似比较水吧,最简单的拆点,直接上代码了. #include <bits/stdc++.h> using namespace std; #define N 1000 #def ...

随机推荐

  1. Qt多线程-总结QThread-QThreadPool-QtConcurrent

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-总结QThread-QThreadPool-QtConcurrent     本文 ...

  2. [微软官网]One Windows Kernel

    One Windows Kernel https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/One-Windows-Kerne ...

  3. PHP中类中成员及常量

    类中成员概述 面向对象编程,是需要通过“对象”去做什么事情(以完成某种任务): 而: 对象总是来源于类: 所以: 面向对象的编程,一切都是从定义类开始: 类中成员分为3大类: 属性: 方法: 常量: ...

  4. excel copy cell & batch operation & checkbox

    excel copy cell & batch operation & checkbox excel 右下角,下拉/双击 (复制 cell) 注意: 不是选择列

  5. 一个Vue实例-添加、显示列表、删除

    <link href="~/Content/css/bootstrap-theme.min.css" rel="stylesheet" /> < ...

  6. MySQL复制 -- Binlog (1)

    复制之所以工作得益于MySQL把对数据库的变更都记录在 binlog中,然后主库把它读出来,放到从库上去应用.当然binlog 的用途不仅限于此,比如 PITR等 在5.1.4版本以前,binlog格 ...

  7. 题解 P1423 【小玉在游泳】

    这道题可以用简单的蒟蒻do-while循环,方式:直到型 因为是萌新/蒟蒻,所以并不知道这道题的时间/空间复杂度是多大 脚踩std(^▽^)摩擦 #include <iostream> # ...

  8. Cows and Cars UVA - 10491 (古典概率)

    按照题目的去推就好了 两种情况 1.第一次选择奶牛的门  概率是 a/(a+b) 打开c扇门后  除去选择的门 还剩 a-1-c+b扇门  则选到车的概率为b/(a-1-c+b) 2.第一次选择车的门 ...

  9. hihoCoder #1639 图书馆

    题目大意 给定 $n$($1\le n\le 1000$)个正整数 $a_1, a_2, \dots, a_n$($a_i \le 10^{12}$),令 $s$ 为这 $n$ 个数之和.求 $$ \ ...

  10. powershell网络钓鱼获取用户密码

    1.powershell网络钓鱼脚本: https://raw.githubusercontent.com/enigma0x3/Invoke-LoginPrompt/master/Invoke-Log ...