题目链接: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 最小费用最大流】 模板的更多相关文章

  1. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  2. 【Luogu】P3381最小费用最大流模板(SPFA找增广路)

    题目链接 哈  学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...

  3. luogu 3376 最小费用最大流 模板

    类似EK算法,只是将bfs改成spfa,求最小花费. 为什么可以呢,加入1-3-7是一条路,求出一个流量为40,那么40*f[1]+40*f[2]+40*f[3],f[1]是第一条路的单位费用,f[2 ...

  4. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  5. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  6. 最大流 && 最小费用最大流模板

    模板从  这里   搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...

  7. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  8. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  9. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

随机推荐

  1. android AIDL服务

    这篇文章http://byandby.iteye.com/blog/1026110我们介绍了android的本地服务:它只能由承载它的应用程序使用.现在我们将介绍如何构建可由其他进程通过 RPC 使用 ...

  2. Enjoy coding

    Enjoy coding iTerm配置 主题选择 Solarized Dark LiquidCarbon 字体选择 Cousine for Powerline(需要安装Powerline字体库), ...

  3. java I/O流 温习随笔

    java I/O流的熟练掌握是十分重要的. 在我的理解中,I/O流可以分为两种:字符流.字节流.字符流就是可以用来传输字符的流,比如传输txt文本,简单的说,只有能被电脑中的记事本直接打开并且你能看懂 ...

  4. (三)HTML中的列表标签、框架集及表单标签

    一.HTML的列表标签 在网页中,经常可以看到,有的内容排列如同word里面的项目编号,这就是HTML的无序排列和有序排列起到的作用.. HTML之无序排列:<ul></ul> ...

  5. SpringMVC:系统认识一下maven

    网上看了很多springMVC helloworld的教程,非常不满意:首先,maven构建的就很少,再者,绝大部分都是断章取义,让人不明就里.其中有几篇讲的好的,我摘录了一些,自己试着构建了一下项目 ...

  6. Android-事件处理机制(待补充)

    http://www.cnblogs.com/plokmju/archive/2013/03/13/2955175.html Android有两条事件处理机制 基于监听的事件处理 基于回调的事件处理 ...

  7. linkedHashMap源码解析(JDK1.8)

    引言 关于java中的不常见模块,让我一下子想我也想不出来,所以我希望以后每次遇到的时候我就加一篇.上次有人建议我写全所有常用的Map,所以我研究了一晚上LinkedHashMap,把自己感悟到的解释 ...

  8. PAT 1055 The World's Richest

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  9. 多结果集IMultipleResult接口

    在某些任务中,需要执行多条sql语句,这样一次会返回多个结果集,在应用程序就需要处理多个结果集,在OLEDB中支持多结果集的接口是IMultipleResult. 查询数据源是否支持多结果集 并不是所 ...

  10. js简单时分秒倒计时

    效果: javascript: <script type="text/javascript"> function countTime() { //获取当前时间 var ...