3.30:

这篇是以前写的,用的还是指针存图,今天又写了个代码,码风稍微好看点。

传送门:http://www.cnblogs.com/Currier/p/6648685.html

---------------------------------------------------一点也不华丽的分割线---------------------------------------------------------

最小费用最大流(洛谷可评测):

 program rrr(input,output);
const
inf=;
type
pointer=^nodetype;
nodetype=record
t,c,w:longint;
next,rev:pointer;
end;
var
a,fre:array[..]of pointer;
p:pointer;
dis,q,frv:array[..]of longint;
v:array[..]of boolean;
n,m,s,t,x,y,c,w,i,sum,f,ans,max:longint;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure ins(x,y,c,w:longint);
begin
new(p);p^.t:=y;p^.c:=c;p^.w:=w;p^.next:=a[x];a[x]:=p;
end;
procedure add(x,y,c,w:longint);
begin
ins(x,y,c,w);ins(y,x,,-w);
a[x]^.rev:=a[y];a[y]^.rev:=a[x];
end;
procedure spfa;
var
h,t:longint;
begin
for i:= to n do dis[i]:=inf;
fillchar(v,sizeof(v),false);
h:=;t:=;q[]:=s;dis[s]:=;v[s]:=true;
while h<>t do
begin
inc(h);if h> then h:=;
p:=a[q[h]];
while p<>nil do
begin
if (p^.c>) and (dis[q[h]]+p^.w<dis[p^.t]) then
begin
dis[p^.t]:=dis[q[h]]+p^.w;
frv[p^.t]:=q[h];fre[p^.t]:=p;
if not v[p^.t] then
begin
inc(t);if t> then t:=;
q[t]:=p^.t;v[p^.t]:=true;
end;
end;
p:=p^.next;
end;
v[q[h]]:=false;
end;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m,s,t);
for i:= to m do begin read(x,y,c,w);add(x,y,c,w); end;
ans:=;max:=;
while true do
begin
spfa;
if dis[t]=inf then break;
i:=t;f:=inf;
while i<>s do begin f:=min(f,fre[i]^.c);i:=frv[i]; end;
max:=max+f;
i:=t;sum:=;
while i<>s do begin fre[i]^.c:=fre[i]^.c-f;fre[i]^.rev^.c:=fre[i]^.rev^.c+f;sum:=sum+fre[i]^.w;i:=frv[i]; end;
ans:=ans+sum*f;
end;
write(max,' ',ans);
close(input);close(output);
end.

最小费用流spfa最小费用路算法模板(pascal)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. 最小费用最大流模板 poj 2159 模板水题

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15944   Accepted: 8167 Descr ...

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

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

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

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

随机推荐

  1. U盘装系统流程_U启动

    步骤 (已有装机U盘的前提下,没有则参考U启动制作U盘启动盘详细教程) (这里默认已设置了相关开机设置,若没设置则参考怎么用u盘装win7系统(推荐看)中步骤11-13) 按开机键后,不断按F12(有 ...

  2. 20155317 2016-2017-2《Java程序设计》课程总结

    20155317 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 新玮的首发博客:对师生关系的期望. C语言与java 20155317 王新玮第二次:语言掌握调查 ...

  3. noone is not in the sudoers file ubuntu

      Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...

  4. Yii2 模块module笔记

    包含内容: 使用GII新建module 建立子模块 在其他控制器中调用模块的操作(action) 1. 使用Gii工具新建module 注意模块的路径,我们没有写backend\modules\Art ...

  5. c++中的stack实现

    通用.类型安全.模板 简直就是巧夺天工的例子

  6. 强化学习读书笔记 - 13 - 策略梯度方法(Policy Gradient Methods)

    强化学习读书笔记 - 13 - 策略梯度方法(Policy Gradient Methods) 学习笔记: Reinforcement Learning: An Introduction, Richa ...

  7. rocketmq Lock failed,MQ already started -c参数

    今天部署rocketmq集群时一台机器部署一个master 和slave,slave部署总是失败,通过查看日志显示下面的错误 java.lang.RuntimeException: Lock fail ...

  8. 各web服务器的特点和优势

    1.Tomcat 和 Jetty 面向java语言 天生就是重量级的web服务器.性能一般 2.IIS 只能在windows平台运行,windows作为服务器在稳定性与其他一些性能上不如类unix操作 ...

  9. 【MySQL 数据库】MySQL目录

    目录 [第一章]MySQL数据概述 [第二章]MySQL数据库基于Centos7.3-部署 [MySQL解惑笔记]Centos7下卸载彻底MySQL数据库 [MySQL解惑笔记]忘记MySQL数据库密 ...

  10. PRML学习笔记第一章

    [转] PRML笔记 - 1.1介绍 模式识别的目标 自动从数据中发现潜在规律,以利用这些规律做后续操作,如数据分类等. 模型选择和参数调节 类似的一族规律通常可以以一种模型的形式为表达,选择合适模型 ...