Codeforces 362E Petya and Pipes 费用流建图
题意:
给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大。
费用流:在某条边增加单位流量的费用。
那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0
同时另一条边(u,v,K,1)费用为1,那么就可以通过限制在增广时相应的费用即可找出最大流
个人觉得这样做的原因是每次增光都是最优的。所以通过限制最终费用不超过K可以得到最优解
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct node
{
int u,v,next;
int flow,cap,cost;
}edge[MAXN * MAXN * ];
int cnt,src,tag;
int C,F;
int K,N;
queue<int>q;
bool inq[MAXN];int d[MAXN];
int head[MAXN],p[MAXN];
int tot = ; void init()
{
memset(head,-,sizeof(head));
tot = ;
} void add_edge(int u,int v,int cap,int cost)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = cap;
edge[cnt].flow = ;
edge[cnt].cost = cost;
edge[cnt].next = head[u];
head[u] = cnt++;
//反向
edge[cnt].v = u;
edge[cnt].u = v;
edge[cnt].flow = ;
edge[cnt].cap = ;
edge[cnt].cost = - cost;
edge[cnt].next = head[v];
head[v] = cnt++;
} bool SPFA(int s, int t)
{
while (!q.empty()) q.pop();
memset(inq,false,sizeof(inq));
memset(d,0x3f,sizeof(d));
memset(p,-,sizeof(p));
d[s] = ;
q.push(s);
inq[s] = true;
while (!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] > d[u] + edge[i].cost && edge[i].cap > edge[i].flow)
{
d[v] = d[u] + edge[i].cost;
p[v] = i;
if (!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
if(d[tag] == INF) return false;
int a = INF;
for (int i = p[tag]; i != -; i = p[edge[i].u])
a = min(a,edge[i].cap - edge[i].flow);
if(C + d[tag] * a > K)
{
F += (K - C) / d[tag];
return false;
}
return true;
}
void slove()
{
C = F = ;
while(SPFA(src,tag))
{
int a = INF;
for (int i = p[tag]; i != -; i = p[edge[i].u])
a = min(a,edge[i].cap - edge[i].flow);
for (int i = p[tag]; i != -; i = p[edge[i].u])
{
edge[i].flow += a;
edge[i ^ ].flow -= a;
}
C += d[tag] * a;
F += a;
}
} int main()
{
while (scanf("%d%d",&N,&K) != EOF)
{
init();
for (int i = ; i <= N ; i++)
for (int j = ; j <= N ; j++)
{
int x;
scanf("%d",&x);
if (x)
{
add_edge(i,j,x,);
add_edge(i,j,K,);
}
}
src = ;
tag = N;
slove();
printf("%d\n",F);
}
return ;
}
Codeforces 362E Petya and Pipes 费用流建图的更多相关文章
- CodeForces 362E Petya and Pipes
Petya and Pipes Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- 「SNOI2019」通信 分治优化费用流建图
题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...
- POJ2516K次费用流建图
Description: N个订单(每个订单订K种商品),M个供应商(每个供应商供应K种商品),K种商品,后N行,表示每一个订单的详细信息,后M行表示每个供应商供应的详细信息,后K 个N * M的矩阵 ...
- hdu4411 经典费用里建图
题意: 给以一个无向图,0 - n,警察在0,他们有k个警队,要派一些警队去1--n个城市抓小偷, 问所有吧所有小偷全抓到然后在返回0的最小路径和是多少,当地i个城市被攻击的时候他会通知i ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙
/** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi ...
- poj 3281 最大流+建图
很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
随机推荐
- 验证码 java实现的程序
makeCheckcode.java package pic; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- 01-Mysql数据库----前戏
MySql的前戏 在学习Mysql之前,我们先来想一下一开始做的登录注册案例,当时我们把用户的信息保存到一个文件中: #用户名 |密码root|123321 alex|123123 上面文件内容的规则 ...
- 最短路径——Bellman-Ford算法
一.相关定义 最短路径:求源点到某特定点的最短距离 特点:Bellman-Ford算法主要是针对有负权值的图,来判断该图中是否有负权回路或者存在最短路径的点 局限性:算法效率不高,不如SPFA算法 时 ...
- 【转】webpack4
1.不再支持node.js4.X 2.不能用webpack命令直接打包指定的文件,只能使用webpack.config.js进行配置. 即:webpack demo01.js bundle01.j ...
- Linux SPI总线和设备驱动架构之二:SPI通用接口层
通过上一篇文章的介绍,我们知道,SPI通用接口层用于把具体SPI设备的协议驱动和SPI控制器驱动联接在一起,通用接口层除了为协议驱动和控制器驱动提供一系列的标准接口API,同时还为这些接口API定义了 ...
- Java相关配置合集
Java环境变量配置: 1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为C:\java\jdk1.6.0_08: 2.安装完成后,右击“我的电脑”,点击“属性”: 3.XP选 ...
- SRM709 div1 Xscoregame(状压dp)
题目大意: 给定一个序列a,包含n个数(n<=15),每个数的大小小于等于50 初始时x = 0,让你每次选a中的一个数y,使得x = x + x^y 问如何安排选择的次序,使得最终结果最大. ...
- CF869E The Untended Antiquity 解题报告
CF869E The Untended Antiquity 题目描述 \(\text{Adieu l'ami}\). Koyomi is helping Oshino, an acquaintance ...
- 线程 condition_variable
http://www.cnblogs.com/haippy/p/3252041.html 理解wait():当前线程调用 wait() 后将被阻塞(此时当前线程应该获得了锁(mutex).在线程被阻塞 ...
- TypeConverter使用
如下代码, <Window.Resources> <local:Human x:Key="human" Name="Tester1" Chil ...