POJ 2516 Minimum cost or minimum charge flow

Source: Internet
Author: User

Test instructions

There are k items, n stores and M stations, each store has a demand for each commodity shopneed[n][k], each depot has a stock of each commodity supply[m][k], for the category K items, he from the supply station J to store I cost Cost[k][i][j], Ask each store to meet the minimum cost of demand for each item.

Analysis:

The typical minimum cost maximum flow problem, but because each commodity's demand and the inventory quantity are very small (<=3), each point splits the new point also to be few, therefore may use the +km method to do.

Code:

POJ 2516//sep9#include <iostream>using namespace Std;const int Maxn=220;int w[maxn][maxn];int mapX[maxN],mapY[ Maxn];int lx[maxn],ly[maxn],linky[maxn];int visx[maxn],visy[maxn];int slack[maxn];int nx,ny,n,m,k;int ShopNeed[maxN ][maxn];int supply[maxn][maxn];int cost[maxn][maxn][maxn];bool Find (int x) {visx[x]=true;for (int y=1;y<=ny;++y) { if (Visy[y]) continue;int t=lx[x]+ly[y]-w[x][y];if (t==0) {visy[y]=true;if (linky[y]==-1| | Find (Linky[y])) {Linky[y]=x;return true;}} else if (slack[y]>t) slack[y]=t;} return false;} int KM () {int I,j;memset (linky,-1,sizeof (Linky)), memset (ly,0,sizeof (ly)); for (i=1;i<=nx;++i) for (J=1,lx[i]=int_ MIN;J&LT;=NY;++J) if (W[i][j]>lx[i]) lx[i]=w[i][j];for (int x=1;x<=nx;++x) {for (i=1;i<=ny;++i) slack[i]=INT_ Max;while (1) {memset (visx,0,sizeof (VISX)); Memset (Visy,0,sizeof (Visy)); if (find (x)) Break;int d=int_max;for (i=1;i <=ny;++i) if (!visy[i]&&slack[i]<d) d=slack[i]; if (D==int_max) return 1;for (I=1;i<=nx;++i) if (Visx[i]) lx[i]-=d;for (i=1;i<=ny;++i) if (Visy[i]) Ly[i]+=d;elseslack[i]-=d;}} int Result=0;for (i=1;i<=ny;++i) if (linky[i]>-1) Result+=w[linky[i]][i];return result; int main () {while (scanf ("%d%d%d", &n,&m,&k) ==3) {if (n==0&&m==0&&k==0) Break;int i,j,t; for (I=1;i<=n;++i) for (t=1;t<=k;++t) scanf ("%d", &shopneed[i][t]); for (j=1;j<=m;++j) for (t=1;t<=k;++ T) scanf ("%d", &supply[j][t]); for (t=1;t<=k;++t) for (i=1;i<=n;++i) for (j=1;j<=m;++j) scanf ("%d",& COST[T][I][J]); int ans=0,p,q;for (t=1;t<=k;++t) {nx=0;ny=0;for (i=1;i<=n;++i) for (p=1;p<=shopneed[i][t];++ p) mapx[++nx]=i;for (J=1;J&LT;=M;++J) for (q=1;q<=supply[j][t];++q) mapy[++ny]=j;for (i=1;i<=nx;++i) for (j=1;j &LT;=NY;++J) W[i][j]=-cost[t][mapx[i]][mapy[j]];int x=km (); if (x>0) {ans=-1;break;} Elseans-=x;} printf ("%d\n", ans);}  return 0;}


POJ 2516 Minimum cost or minimum charge 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.