POJ 2526 Minimum Cost "minimum charge maximum Flow"

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2516

Test instructions
N Shop owner m suppliers k kinds of goods to the owner of the K-type goods and the supplier of K-type cargo stock and K type of transportation costs.

Solution: The K-cost flow, respectively, the cost of each kind of goods. Source point to the supply side, the shopkeeper to the meeting point to build the edge, the cost is 0, the capacity of 1. Then supply to the shopkeeper to build the side, the cost of the matrix, the capacity is infinite.

Code:

/*poj 2195 going home adjacency matrix form minimum cost maximum flow * /#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>using namespace STD;//************************************************************//Minimum cost maximum flow algorithm//SPFA to find the shortest way//adjacency matrix form//Initialize: Cap: Capacity, no edge for 0//cost: Cost, symmetrical form, 0 without side//c is the minimum cost//f is the maximum flow//*******************************************************Const intMAXN = -;Const intINF =0x3fffffff;intCAP[MAXN][MAXN];//capacity, No side for 0intFLOW[MAXN][MAXN];//cost matrix is symmetrical, there is a charge of I to J, then the cost of J to I is the opposite numberintCOST[MAXN][MAXN];//CostintN///vertex number 0~n-1intF//Maximum flowintC//Minimum costintStart, End;//Source point and meeting pointBOOLVIS[MAXN];//In the queue flagintQUE[MAXN];intPRE[MAXN];intDIST[MAXN];//s-t path Minimum costBOOLSPFA () {intFront =0, rear =0; for(intU =0; U <= N; u++) {if(U = = start)            {que[rear++] = u; Dist[u] =0; Vis[u] =true; }Else{Dist[u] = INF; Vis[u] =false; }    } while(Front! = rear) {intU = que[front++]; Vis[u] =false;if(Front >= maxn) front =0; for(intv =0; V <= N; v++) {if(Cap[u][v]>flow[u][v] && Dist[v]>dist[u] + cost[u][v])                {Dist[v] = Dist[u] + cost[u][v]; PRE[V] = u;if(!vis[v]) {Vis[v] =true; que[rear++] = v;if(Rear >= MAXN) rear =0; }            }        }    }if(Dist[end] >= INF)return false;return true;}voidMincostmaxflow () {memset(Flow,0,sizeof(flow)); c = f =0; while(SPFA ()) {intMin = INF; for(intu = End; U! = start; u = pre[u]) min = min (min, cap[pre[u]][u]-flow[pre[u]][u]); for(intu = End; U! = start;            u = pre[u]) {Flow[pre[u]][u] + = Min;        Flow[u][pre[u]] = Min;        } c + = dist[end] * Min;    f + = Min; }}//************************************************************inttmpinta[10000][ -];intb[10000][ -];intMain () {intN, M, K; while(~scanf("%d%d%d", &n, &m, &k)) {if(N = =0&& M = =0&& K = =0) Break;memsetA0,sizeof(a));memset(b,0,sizeof(b)); Start =0; n = n + m+2; End = M + N +1;intNeed =0; for(inti =1; I <= N; i++) { for(intK =1; K <= k;k++) {scanf("%d", &a[i][k]);            Need + = A[i][k]; }        } for(inti =1; I <= M; i++) { for(intK =1; K <= K; k++) {scanf("%d", &b[i][k]); }        }intAns =0;intres =0; for(intKK =1; KK <= K; kk++) {memset(Cap,0,sizeof(CAP));memset(Cost,0,sizeof(cost)); for(inti =1; I <= M; i++)//source points to the supply point to build edgesCap[start][i] = B[i][kk]; for(inti =1; I <= N; i++)//Shopkeeper to the meeting point to build the sideCap[m + i][end] = A[i][kk]; for(inti =1; I <= N; i++) for(intj =1; J <= M; J + +) {scanf("%d", &tmp);                    Cost[j][i + M] = tmp;                    Cost[i + m][j] =-tmp; Cap[j][i + M] =1000000;            } mincostmaxflow ();            Ans + = c;        Res + = f; }if(res = = need)printf("%d\n", ans);Else            printf(" -1\n"); }return 0;}

Copyright NOTICE: Reprint please indicate the source.

Poj 2526 Minimum cost min. Max Flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.