Topic Links:
Poj 2516 Minimum Cost
Title Description:
There are n stores, M warehouses, and each store and warehouse has K-type goods. Why Now N stores are starting to send orders to M warehouses, and the order information is the current store demand for each type of goods. Different stores from different warehouses to buy different goods cost different, ask is not able to meet all the requirements of the store, if the total cost of the store to the minimum amount?
Problem Solving Ideas:
Simple cost flow, to run K times the minimum cost of the maximum flow, each time only a kind of goods to build a chart run cost flow. Each build takes 0 as the source point, [1,m] is a warehouse, [M+1, N+m] is a store, and N+m+1 is a meeting point. 0 with [1,m] The edge, the edge capacity for the warehouse inventory, the cost of the side unit traffic is 0.
[1,m] with [m+1,n+m] edge, edge capacity for INF, the cost of the side unit traffic for the corresponding warehouse supply I goods to the unit of the corresponding store cost. [M+1,n+m] with the sink point edge, the edge capacity for the store demand, the cost of the side unit traffic is 0. The topic of the map when the initialization must be done well, or it is easy to tle.
1#include <queue>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8 Const intMAXN = About;9 Const intINF =0x3f3f3f3f;Ten intFLOW[MAXN][MAXN], CAP[MAXN][MAXN]; One intSHOP[MAXN][MAXN], SUPPLY[MAXN][MAXN]; A intVIS[MAXN], PRE[MAXN], DIS[MAXN], COST[MAXN][MAXN]; - intC, Flow, S, E; - the BOOLSPFA () - { -Queue <int>Q; - for(intI=s; i<=e; i++) + { -Dis[i] =INF; +Vis[i] =0; A } atdis[0] =0; -vis[0] =1; - Q.push (s); - while(!q.empty ()) - { - intU =Q.front (); in Q.pop (); -Vis[u] =0; to for(intV=s; v<=e; v++) + { - if(Cap[u][v]>flow[u][v] && dis[v]>dis[u]+Cost[u][v]) the { *DIS[V] = Dis[u] +Cost[u][v]; $PRE[V] =u;Panax Notoginseng if(!Vis[v]) - { theVIS[V] =1; + Q.push (v); A } the } + } - } $ if(Dis[e] = =INF) $ return false; - return true; - } the - voidMincostmaxflow ()Wuyi { thememset (Flow,0,sizeof(flow)); -c = Flow =0; Wu while(SPFA ()) - { About intMin =INF; $ for(intI=e; I!=s; I=Pre[i]) -min = min (min, cap[pre[i]][i]-flow[pre[i]][i]); - for(intI=e; I!=s; I=Pre[i]) - { AFlow[pre[i]][i] + =Min; +Flow[i][pre[i]]-=Min; the } -Flow + =Min; $c + = Min *Dis[e]; the } the } the the intMain () - { in intN, M, K; the while(SCANF (" %d%d%d", &n, &m, &k), n| | m| |k) the { About intflag, cost; theFlag = Cost = s =0, E = n + M +1; the the for(intI=1; i<=n; i++) + for(intj=1; j<=k; J + +) -scanf ("%d", &shop[i][j]); the Bayi for(intI=1; i<=m; i++) the for(intj=1; j<=k; J + +) thescanf ("%d", &supply[i][j]); - -memset (Cap,0,sizeof(CAP)); thememset (Cost,0,sizeof(cost)); the for(intI=1; i<=m; i++) the for(inti=n+1; j<=n+m; J + +) theCAP[I][J] =INF; - the for(intI=1; i<=k; i++) the { the intTotal =0;94 for(intj=1; j<=n; J + +) the for(intL=1; l<=m; l++) the { thescanf ("%d", &cost[l][j+m]);98COST[J+M][L] =-cost[l][j+m]; About } - 101 if(flag)102 Continue;103 104 for(intj=1; j<=n; J + +) the {106Cap[j+m][e] =Shop[j][i];107Total + =Shop[j][i];108 }109 for(intj=1; j<=m; J + +) theCAP[S][J] =Supply[j][i];111 the Mincostmaxflow ();113 if(Flow <Total ) theFlag =1; theCost + =C; the 117 }118printf ("%d\n", flag?-1: Cost);119 } - return 0;121}
Poj 2516 Minimum Cost (min. maximum flow)