# 最小割&费用流

#### 费用流：

##### 模板：
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 1e6 + 5;
int h[maxn],n,m,s,t,cnt = 2;
int pre[maxn],incf[maxn],dis[maxn];//pre用于记录路径，incf记录流，dis记录费用
ll maxFlow,maxCost;
bool vis[maxn];
struct node{
int cost,e,nex,flow;
}e[maxn * 2];
void add(int a,int b,int f,int cost){
e[cnt].e = b;
e[cnt].nex = h[a];
e[cnt].cost = cost;
e[cnt].flow = f;
h[a] = cnt++;
}
bool spfa(){//spfa
for(int i = 1; i <= n; i++)vis[i] = 0,dis[i] = INF;
vis[s] = 1,dis[s] = 0,incf[s] = INF;
queue<int> qu;
qu.push(s);
while(!qu.empty()){
int tmp = qu.front();
vis[tmp] = 0;
qu.pop();
for(int i = h[tmp]; i ; i = e[i].nex){
int endd = e[i].e;
if(!e[i].flow)continue;//如果没残余流量了就不走
if(dis[endd] > dis[tmp] + e[i].cost){
pre[endd] = i;
dis[endd] = dis[tmp] + e[i].cost;
incf[endd] = min(incf[tmp],e[i].flow);
if(!vis[endd])qu.push(endd),vis[endd] = 1;
}
}
}
if(dis[t] == INF)return 0;
else return 1;
}

void MCMF(){//MCMF本体
while(spfa()){
int x = t;
maxCost += (ll)dis[t] * incf[t];//最小费
maxFlow += incf[t];//最大流
while(x != s){//更新所有路径的流的状况
e[pre[x]].flow -= incf[t];
e[pre[x] ^ 1].flow += incf[t];
x = e[pre[x] ^ 1].e;
}
}
}
/*
4 5 1 4
3 1 3
3 1 2
2 3 2
3 4 3
4 2 1
*/

10-08
04-23

05-11
05-16
02-17
05-29