#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #define INF 0x3f3f3f3f using namespace std; struct edge{ int from,to,cap,cost,next; }E[600000]; int tot; int head[300];int dis[300]; int inq[300]; int pre[300]; int maxf = 0; void addedge(int a,int b,int cap, int cost) { E[tot].from = a; E[tot].to = b; E[tot].cap = cap; E[tot].cost = cost; E[tot].next = head[a]; head[a] = tot++; //反向边 E[tot].from = b; E[tot].to = a; E[tot].cap = 0; E[tot].cost = -cost; E[tot].next = head[b]; head[b] = tot++; } void init() { memset(E,0,sizeof(E)); memset(head,-1,sizeof(head)); memset(pre,-1,sizeof(pre)); tot = 0; maxf = 0; }bool spfa(int b , int e) { memset(dis,INF,sizeof(dis)); memset(inq,false,sizeof(inq)); memset(pre, -1, sizeof(pre)); queue<int> q; dis[b] = 0; inq[b] = true; q.push(b); while (!q.empty()) { int u = q.front() ;q.pop(); for (int i = head[u]; i!=-1 ; i = E[i].next) { int v = E[i].to; if (E[i].cap && dis[v] > dis[u] + E[i].cost) { dis[v] = dis[u] + E[i].cost; pre[v] = i; if (!inq[v]) {q.push(v);inq[v] = true;} } } inq[u] = false; } return pre[e]!=-1; } int MCMF(int b ,int e) { int ANS = 0; while (spfa(b,e)) { int minf = INF; for (int i = e ; i!=b ; i = E[pre[i]].from) { minf = min(minf , E[pre[i]].cap); } for (int i = e ; i!=b ; i = E[pre[i]].from) { E[pre[i]].cap -= minf; E[pre[i]^1].cap += minf; } maxf += minf; ANS += (minf*dis[e]); } return ANS; } int main() { initfirst(); init(); MCMF(b,e); }