标签:
1 bool spfa(int s, int t) { 2 queue<int> Q; 3 Q.push(s); 4 rst(d, inf); 5 rst(p, -1); 6 rst(pp, -1); 7 d[s] = 0; 8 vis[s] = true; 9 while(!Q.empty()) { 10 int e = Q.front(); Q.pop(); 11 vis[e] = false; 12 for(int i = head[e]; i+1; i = edge[i].next) { 13 if(edge[i].w) { 14 int v = edge[i].v; 15 if(d[v] > d[e] + edge[i].c) { 16 d[v] = d[e] + edge[i].c; 17 p[v] = e; 18 pp[v] = i; 19 if(!vis[v]) { 20 Q.push(v); 21 } 22 } 23 } 24 } 25 } 26 if(d[t] == INF) return false; 27 return true; 28 } 29 int min_cost_flow(int s, int t) { 30 int ans_flow = 0, ans_cost = 0; 31 int u, mn; 32 while(spfa(s, t)) { 33 u = t; 34 mn = INF; 35 while(p[u] != -1) { 36 mn = min(mn, edge[pp[u]].w); 37 u = p[u]; 38 } 39 u = t; 40 while(p[u] != -1) { 41 edge[pp[u]].w -= mn; 42 edge[pp[u]^1].w += mn; 43 u = p[u]; 44 } 45 ans_cost += d[t] * mn; 46 ans_flow += mn; 47 } 48 return ans_cost; 49 }
标签:
原文地址:http://www.cnblogs.com/mitrenick/p/4456221.html