nm无影响 s为源点 t为汇点
最大流为maxflow 最小费用为 mincost

#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// const int N=100001; int n,m,S,T,k,maxflow,mincost,last[N],pre[N],dis[N],flow[N]; bool vis[N]; struct Edge{ int next,to,flow,dis; }edge[N<<1]; int pos=1,head[N]; void init() { pos=1; CLR(head,0); mincost=maxflow=0; } queue <int> q; int id(int x,int y) {return n*(x-1)+y;} void add(int from,int to,int flow,int dis)//flow流量 dis费用 { edge[++pos].next=head[from]; edge[pos].flow=flow; edge[pos].dis=dis; edge[pos].to=to; head[from]=pos; edge[++pos].next=head[to]; edge[pos].flow=0; edge[pos].dis=-dis; edge[pos].to=from; head[to]=pos; } bool spfa(int s,int t) { CLR(dis,0x3f); CLR(flow,0x3f); CLR(vis,0); while (!q.empty()) q.pop(); dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1; int tot=0; while (!q.empty()) { int now=q.front(); q.pop(); vis[now]=0; for (int i=head[now]; i; i=edge[i].next) { int to=edge[i].to; if (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis) { dis[to]=edge[i].dis+dis[now]; flow[to]=min(edge[i].flow,flow[now]); last[to]=i; pre[to]=now; if (!vis[to]) { q.push(to); vis[to]=1; } } } } return pre[t]!=-1; } void MCMF(int s,int t) { while (spfa(s,t)) { int now=t; maxflow+=flow[t]; mincost+=flow[t]*dis[t]; while (now!=s) { edge[last[now]].flow-=flow[t];//dis . flow edge[last[now]^1].flow+=flow[t]; now=pre[now]; } } }