BZOJ 1061 志愿者招募 最小费用流&&线性规划建模
题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=1061
题目大意:
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
#define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Mem(a) memset(a, 0, sizeof(a))
#define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
#define MID(l, r) ((l) + ((r) - (l)) / 2)
#define lson ((o)<<1)
#define rson ((o)<<1|1)
#pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
typedef long long ll;
const int maxn = + ;
const int mod = ;//const引用更快,宏定义也更快
const int INF = 1e9; struct edge
{
int u, v, c, f, cost;
edge(int u, int v, int c, int f, int cost):u(u), v(v), c(c), f(f), cost(cost){}
};
vector<edge>e;
vector<int>G[maxn];
int a[maxn];//找增广路每个点的水流量
int p[maxn];//每次找增广路反向记录路径
int d[maxn];//SPFA算法的最短路
int inq[maxn];//SPFA算法是否在队列中
int n, m;
void init(int n)
{
for(int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c, int cost)
{
e.push_back(edge(u, v, c, , cost));
e.push_back(edge(v, u, , , -cost));
int m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
bool bellman(int s, int t, int& flow, long long & cost)
{
for(int i = ; i <= n + ; i++)d[i] = INF;//Bellman算法的初始化
memset(inq, , sizeof(inq));
d[s] = ;inq[s] = ;//源点s的距离设为0,标记入队
p[s] = ;a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的) queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
inq[u] = ;//入队列标记删除
for(int i = ; i < G[u].size(); i++)
{
edge & now = e[G[u][i]];
int v = now.v;
if(now.c > now.f && d[v] > d[u] + now.cost)
//now.c > now.f表示这条路还未流满(和最大流一样)
//d[v] > d[u] + e.cost Bellman 算法中边的松弛
{
d[v] = d[u] + now.cost;//Bellman 算法边的松弛
p[v] = G[u][i];//反向记录边的编号
a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
if(!inq[v]){q.push(v);inq[v] = ;}//Bellman 算法入队
}
}
}
if(d[t] == INF)return false;//找不到增广路
flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
cost += (long long)d[t] * (long long)a[t];//距离乘上到达汇点的流量就是费用
for(int u = t; u != s; u = e[p[u]].u)//逆向存边
{
e[p[u]].f += a[t];//正向边加上流量
e[p[u] ^ ].f -= a[t];//反向边减去流量 (和增广路算法一样)
}
return true;
}
int MincostMaxflow(int s, int t, long long & cost)
{
cost = ;
int flow = ;
while(bellman(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
return flow;//返回最大流,cost引用可以直接返回最小费用
}
int main()
{
int U = 1e5;
scanf("%d%d", &n, &m);
int s = , t = n + , x;
addedge(s, , U, );//从源点到出发点连边 流量为U
for(int i = ; i <= n; i++)
{
scanf("%d", &x);
addedge(i, i + , U - x, );//把需要的人当做坑来填满 这样流到汇点t的流量会小于U
}
int u, v, w;
for(int i = ; i <= m; i++)//对于每种类型的员工,连边u->v+1 这样可以保证从u出发到v+1的流量增加 也就是填坑
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v + , INF, w);//流量为INF 费用为w
}
long long cost;
MincostMaxflow(s, t, cost);
printf("%lld\n", cost);
return ;
}
BZOJ 1061 志愿者招募 最小费用流&&线性规划建模的更多相关文章
- bzoj 1061 志愿者招募(最小费用最大流)
[Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3792 Solved: 2314[Submit][Status][Di ...
- BZOJ 1061 志愿者招募(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...
- bzoj 1061 志愿者招募 有上下界费用流做法
把每一天看作一个点,每一天的志愿者数目就是流量限制,从i到i+1连边,上下界就是(A[i],+inf). 对于每一类志愿者,从T[i]+1到S[i]连边,费用为招募一个志愿者的费用,流量为inf.这样 ...
- BZOJ 1061 志愿者招募
http://www.lydsy.com/JudgeOnline/problem.php?id=1061 思路:可以用不等式的改装变成费用流. 将不等式列出,如果有负的常数,那么就从等式连向T,如果是 ...
- bzoj 1061 志愿者招募 费用流
详见BYV的博客,写的非常全面https://www.byvoid.com/blog/noi-2008-employee /************************************** ...
- 【BZOJ 1061】 1061: [Noi2008]志愿者招募 (线性规划与网络流)**
1061: [Noi2008]志愿者招募 Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短 ...
- bzoj [Noi2008] 1061 志愿者招募 单纯形
[Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5437 Solved: 3267[Submit][Status][Di ...
- BZOJ 3265 志愿者招募加强版(单纯形)
3265: 志愿者招募加强版 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 848 Solved: 436[Submit][Status][Disc ...
- BZOJ 1061: [Noi2008]志愿者招募(线性规划与网络流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1061 题意: 思路: 直接放上大神的建模过程!!!(https://www.byvoid.com/z ...
随机推荐
- [转].Net Windows服务安装完成后自动启动
本文转自:http://www.cnblogs.com/hb_cattle/archive/2011/12/04/2275319.html 考虑到部署方便,我们一般都会将C#写的Windows服务制作 ...
- c#尽量使用条件属性(Conditional Attribute)
至此我们应该对Attribute属性大体了解了.下面来看看条件属性(Conditional Attribute)到底是怎么回事. 1 [Conditional("DEBUG")] ...
- c#参数修饰符-ref
ref 关键字通过引用传递参数. 方法定义和调用方法必须显式使用ref关键字: 调用方法时参数必须初始化: 参数中可以声明多个ref修饰的参数. 例: public void UseRef( ref ...
- django中的验证码
from django.shortcuts import renderfrom PIL import Imagefrom PIL import ImageDrawfrom PIL import Ima ...
- 配置/etc/profile错误导致很多系统命令无法使用
在配置hadoop的环境变量的过程中,由于字符输入错误导致/etc/profile文件出错,并导致系统的基本命令不能使用,如:vi,ls等. 这种情况,首先修改/etc/profile的错误文件内容, ...
- 判断php变量是否定义,是否为空,是否为真的一览表
分类: 使用 PHP 函数对变量 $x 进行比较 表达式 gettype() empty() is_null() isset() boolean : if($x) $x = ""; ...
- shell文本操作
一.find查找命令的使用 1.find . -name "*.txt" 在当前目录下,查找以txt结尾的文件 2.find . -name "[a-z]" 在 ...
- SQL Server 2008各版本介绍区别(包含企业版 开发者版 标准版 Web版 工作组版 Express版 Compact版)
SQL Server 2008分为SQL Server 2008企业版.标准版.工作组版.Web版.开发者版.Express版.Compact 3.5版,其功能和作用也各不相同,其中SQL Serve ...
- #if, #elif, #else, #endif 使用
程序想要通过简单地设置一些参数就生成一个不同的软件,在不同的情况下可能只用到一部分代码,就没必要把所有的代码都写进去,就可以用条件编译,通过预编译指令设置编译条件,在不同的需要时编译不同的代码. (一 ...
- [SHOI2007]园丁的烦恼
裸的二维数点 #include"cstdio" #include"cstring" #include"iostream" #include& ...