题目链接: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. oracle trim不掉空白字符分享(转)

    本文转自:http://www.2cto.com/database/201306/223558.html 问题背景:一个工商注册号,正常的用trim能解决的问题,但是这个case,trim后和肉眼看到 ...

  2. hrb——开锁魔法I——————【规律】

    解题思路:从1到n的倒数之和. #include<stdio.h> #include<string.h> #include<algorithm> using nam ...

  3. Shell脚本检测程序,如果挂了就重启程序

    脚本如下: #!/bin/sh #要检查的进程名 PROGRESS_NAME="heihu_server" #----------------------------------- ...

  4. 设置session超时的三种方式

    设置session超时的三种方式 1. 在容器中设置:如在tomcat-7\conf\web.xml中设置 Tomcat默认session超时时间为30分钟,可以根据需要修改,负数或0为不限制sess ...

  5. 正则表达式把所有Paul替换成Ringo:Paul Puala Pualine paul Paul

    代码实现如下: <!DOCTYPE html><html><body> <h2>JavaScript Regular Expressions</h ...

  6. mardown文档的用法

    <hr>分割换行符<br>分隔符 <ul> <li></li></ul>无序标签 <ol><li>< ...

  7. 常用DOM结构方法总结

    ---内容开始--- 获取元素的方法: getElementById() 通过ID名获取元素 getElementsByTagName() 通过元素(标签)名称 getElementsByClassN ...

  8. Steps of source code change to executable application

    程序运行的整个过程,学习一下 源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 汇编程序 (assembler) → 目标代码 (o ...

  9. Prime Numbers in a Grid素数网格

    &/@ Shorthand notation for Map If[PrimeQ[#], Framed@Style[#, Orange, Bold, 15], #] & /@ Rang ...

  10. Matlab GUI选项卡

    1.在这个网址下载一个工具包,里面应该有四个文件:tabselectionfcp.p.tabselectionfcn.m.tabpanel.p和tabpanel.m,显然代码用.p格式进行加密了. 2 ...