【luogu P3381 最小费用最大流】 模板
题目链接:https://www.luogu.org/problemnew/show/P3381
把bfs变成spfa
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = + ;
int n, m, s, t, maxflow, mincost, dis[maxn], flow[maxn], pre[maxn], last[maxn];
struct edge{
int to, next, cost, flow;
}e[maxn<<];
int head[maxn], cnt = -;
bool vis[maxn];
queue<int> q;
void add(int u, int v, int w, int c)
{
e[++cnt].next = head[u];
e[cnt].to = v;
e[cnt].cost = c;
e[cnt].flow = w;
head[u] = cnt; e[++cnt].next = head[v];
e[cnt].to = u;
e[cnt].cost = -c;
e[cnt].flow = ;
head[v] = cnt;
}
bool SPFA(int s, int t)
{
memset(dis, 0x7f, sizeof(dis));
memset(flow, 0x7f, sizeof(flow));
memset(vis, , sizeof(vis));
q.push(s); pre[t] = -; vis[s] = ; dis[s] = ;
while(!q.empty())
{
int now = q.front(); q.pop();
vis[now] = ;
for(int i = head[now]; i != -; i = e[i].next)
{
if(e[i].flow > && dis[e[i].to] > dis[now] + e[i].cost)
{
dis[e[i].to] = dis[now] + e[i].cost;
pre[e[i].to] = now;
last[e[i].to] = i;
flow[e[i].to] = min(flow[now], e[i].flow);
if(!vis[e[i].to])
{
q.push(e[i].to);
vis[e[i].to] = ;
}
}
}
}
return pre[t] != -;
}
void MCMF(int s, int t)
{
while(SPFA(s, t))
{
int now = t;
maxflow += flow[t];
mincost += flow[t] * dis[t];
while(now != s)
{
e[last[now]].flow -= flow[t];
e[last[now]^].flow += flow[t];
now = pre[now];
}
}
}
int main()
{
memset(head, -, sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i = ; i <= m; i++)
{
int u, v, w, c;
scanf("%d%d%d%d",&u,&v,&w,&c);
add(u, v, w, c);
}
MCMF(s, t);
printf("%d %d\n",maxflow, mincost);
return ;
}
【luogu P3381 最小费用最大流】 模板的更多相关文章
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
- 【Luogu】P3381最小费用最大流模板(SPFA找增广路)
题目链接 哈 学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...
- luogu 3376 最小费用最大流 模板
类似EK算法,只是将bfs改成spfa,求最小花费. 为什么可以呢,加入1-3-7是一条路,求出一个流量为40,那么40*f[1]+40*f[2]+40*f[3],f[1]是第一条路的单位费用,f[2 ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...
- Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
随机推荐
- HikariCP配置使用spring结合--Java数据库连接池
我的个人德州扑克项目https://github.com/mingzijian/pokers,欢迎给星星.maven引入: Java 8 maven artifact: <dependency& ...
- linux_api之信号
本片索引: 1.引言 2.信号 3.程序启动 4.signal函数 5.系统调用的中断和系统调用的重启(了解) 6.可再入与不可再入函数(了解) 7.kill函数和raise函数 8.alarm函数和 ...
- matlab安装过程的被要求的配置程序
顺序是这样的: 网址的顺序是这样的: 1. http://cn.mathworks.com/support/compilers/R2015b/index.html?sec=win64&s_ci ...
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- NPOI之C#下载Excel
Java中这个类库叫POI,C#中叫NPOI,很多从Java一直到.Net平台的类库为了区别大部分都是在前面加个N,比如Hibernate和NHibernate. npoi下载地址 一.使用NPOI下 ...
- Android界面编程--使用活动条(ActionBar)--通过ActionBar菜单改变TextView的字体和颜色
android:orientation="vertical"(AndroidStudio不提示,这个要记住了) 昨天好不容易把ActionBar从溢出菜单overflow中弄出来了 ...
- 多线程TCP的socket通信
应用多线程来实现服务器与多客户端之间的通信. 基本步骤: 1.服务器端创建ServerSocket,循环调用accept()等待客户端的连接 2.客户端创建一个socket并请求和服务器端的连接 3. ...
- redux基本使用
redux数据流向 基本使用
- html表格设置
表格 表格由 <table> 标签来定义.每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义).字母 td 指表格数据(t ...
- eclipse 误删文件的恢复,代码的恢复
误删除文件的恢复 在用eclipse进行代码编写操作时,有时会误删除文件或者文件包.通过eclipse的恢复文件功能可以恢复误删除的文件. 具体步骤为: 1.选择误删除文件在eclipse所在包(文件 ...