标准大白书式模板

 #include<stdio.h>        //大概这么多头文件昂
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxm=+; //最大点数
const int INF=0x3f3f3f3f; struct edge{ //边:起点、终点、容量、流量、单位费用
int from,to,c,f,cost;
edge(int a,int b,int m,int n,int p):from(a),to(b),c(m),f(n),cost(p){}
}; int aabs(int a){
return a>=?a:-a;
} struct MCMF{
int m,s,t;
vector<edge>e;
vector<int>g[maxm];
int dis[maxm],a[maxm],p[maxm];
bool vis[maxm]; void init(int n){ //初始化函数
for(int i=;i<=n;i++)g[i].clear();
e.clear();
} void add(int a,int b,int c,int v){ //加边函数
e.push_back(edge(a,b,c,,v));
e.push_back(edge(b,a,,,-v));
m=e.size();
g[a].push_back(m-);
g[b].push_back(m-);
} bool spfa(int& flow,int& cost){
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=;
dis[s]=;
p[s]=;
a[s]=INF;
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=;
for(int i=;i<g[u].size();i++){
edge tmp=e[g[u][i]];
if(dis[tmp.to]>dis[u]+tmp.cost&&tmp.c>tmp.f){
dis[tmp.to]=dis[u]+tmp.cost;
p[tmp.to]=g[u][i];
a[tmp.to]=min(a[u],tmp.c-tmp.f);
if(!vis[tmp.to]){
q.push(tmp.to);
vis[tmp.to]=;
}
}
}
}
if(dis[t]==INF)return ;
flow+=a[t];
cost+=dis[t]*a[t];
int u=t;
while(u!=s){
e[p[u]].f+=a[t];
e[p[u]^].f-=a[t];
u=e[p[u]].from;
}
return ;
} int MF(int s,int t){ 调用的计算最小费用函数
this->s=s;this->t=t;
int flow=,cost=;
while(spfa(flow,cost));
return cost;
} };

网络流--最小费用最大流MCMF模板的更多相关文章

  1. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

  2. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  3. poj2135最小费用最大流经典模板题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13509   Accepted: 5125 Descri ...

  4. HDU 6118 度度熊的交易计划(网络流-最小费用最大流)

    度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...

  5. 把人都送到房子里的最小花费--最小费用最大流MCMF

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...

  6. HDU 1533 最小费用最大流(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...

  7. 洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流

    题意简述 有一个\(n\times m\)棋盘,棋盘上每个格子上有一个水管.水管共有\(16\)种,用一个\(4\)位二进制数来表示当前水管向上.右.下.左有个接口.你可以旋转除了\((0101)_2 ...

  8. POJ-2516(最小费用最大流+MCMF算法)

    Minimum Cost POJ-2516 题意就是有n个商家,有m个供货商,然后有k种商品,题目求的是满足商家的最小花费供货方式. 对于每个种类的商品k,建立一个超级源点和一个超级汇点.每个商家和源 ...

  9. POJ-2195(最小费用最大流+MCMF算法)

    Going Home POJ-2195 这题使用的是最小费用流的模板. 建模的时候我的方法出现错误,导致出现WA,根据网上的建图方法没错. 这里的建图方法是每次到相邻点的最大容量为INF,而花费为1, ...

随机推荐

  1. Unity 如何在打包的时候执行一些逻辑

    1.如果想让unity在打包的过程中,执行一些逻辑,那么该如何做呢?代码如下: using UnityEditor; using UnityEditor.Build; using UnityEngin ...

  2. 增加centos7.3上安装php7的php-soap扩展

    代码传到正式服务器上去就:Class 'SoapClient' not found,只能是soap扩展没装!    因为服务器上面的PHP是7.1.11的,所以soap也要装7.1.11的,否则会冲突 ...

  3. R & ggplot2 & Excel绘图(直方图/经验分布图/QQ图/茎叶图/箱线图)实例

    持续更新~ 散点图 条形图 文氏图 饼图 盒型图 频率直方图 热图 PCA图 3D图 火山图 分面图 分面制作小多组图 地图 练习数据: year count china Ame jap '12 2. ...

  4. WPF几种高级绑定

    (1)Binding  + RelativeSource + AncestorType 模式  , 根据关联源所指定的类型,可动态绑定指定类型的Path属性(Path可以省略)(PS:动态指父级在运行 ...

  5. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  6. quartz---触发job时间和结束时间

    quartz:Trigger:触发job时间和结束时间 package com.imooc.demo.helloQuartz; import java.text.SimpleDateFormat; i ...

  7. iOS UI-手势(Gesture)

    #import "ViewController.h" @interface ViewController ()<UIActionSheetDelegate> @prop ...

  8. OC NSNumber和NSValue和NSDate和NSData

    一 NSNumber // // main.m // 07-NSNumber // // Created by apple on 13-8-12. // Copyright (c) 2013年 itc ...

  9. CachedThreadPool里的线程是如何被回收的?

    线程池创建线程的逻辑图: 我们分析CachedThreadPool线程池里的线程是如何被回收的. //Executors public static ExecutorService newCached ...

  10. Centos中彻底删除Mysql(rpm、yum安装的情况)

    [root@data lib]# rpm -qa¦grep mysqlmysql-5.5.25-1.el6.remi.i686mysql-libs-5.5.25-1.el6.remi.i686comp ...