POJ 2516 Minimum Cost (最小费用最大流+拆图)

题目

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.

Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.

The input is terminated with three "0"s. This test case should not be processed.
Output
For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".
Sample Input
1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0
Sample Output
4
-1

思路

算法很明显就是mcmf的题目,but如果暴力建图的话必然会t掉,因为我每个供应商的每种货物到达每个商家的花费都不一样,如果都看成点的话肯定是要裂开。那么我们可以很明显观察到,每个商品之间是没有影响的,都是相对独立的,那么我们考虑建k张图那么这样我们就可以直接跑最小费用流了,然后累加答案就好了。

代码实现

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=start;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005 
#define fi first 
#define se second
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f3f3f3f3f;
typedef vector <int> VI;
typedef pair<int ,int> PII;
typedef pair<int,ll> PLL;
typedef pair<int ,PII> PIII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
inline int read() {
    char ch=getchar(); int x=0, f=1;
    while(ch<'0'||ch>'9') {
        if(ch=='-') f=-1;
        ch=getchar();
    } while('0'<=ch&&ch<='9') {
        x=x*10+ch-'0';
        ch=getchar();
    } return x*f;
}
const int maxn=5e4+10;
int n,m,s,t,cnt=1;
int head[maxn],dis[maxn],pre[maxn],incf[maxn];
int maxflow,mincost;
bool vis[maxn];
struct edge {
    int v,next,flow,cost;
}e[maxn];

inline void add (int u,int v,int flow,int cost) {
    e[++cnt]= (edge) {v,head[u],flow,cost};
    head[u]=cnt;
}

inline void add_edge (int u,int v,int flow,int cost) {
   add (u,v,flow,cost);
   add (v,u,0,-cost);
}

inline bool spfa () {
    queue <int > q;
    MT (dis,0x3f);
    MT (vis,0);
    q.push (s);
    dis[s]=0;
    vis[s]=1;
    incf[s]=1<<30;
    while (q.size ()) {
        int x=q.front (); q.pop ();
        vis[x]=0;
        for (int i=head[x];~i;i=e[i].next) {
           if (!e[i].flow) continue;
           int v=e[i].v;
           if (dis[v]>dis[x]+e[i].cost) {
               dis[v]=dis[x]+e[i].cost;
               incf[v]=min (incf[x],e[i].flow);
               pre[v]=i;
               if (!vis[v]) vis[v]=1,q.push (v);
           }
        }
    }
    if (dis[t]==inf) return 0;
    return dis[t];
}

inline void mcmf () {
    while (spfa ()) {
       int x=t;
       maxflow+=incf[t];
       mincost+=dis[t]*incf[t];
       int i;
       while (x!=s) {
           i=pre[x];
           e[i].flow-=incf[t];
           e[i^1].flow+=incf[t];
           x=e[i^1].v;
       }
    }
}

const int M=60;
int need[M][M],have[M][M];
int board[M];
int price[M][M][M];
int k;

int main () {
    while (scanf ("%d%d%d",&n,&m,&k)&&(n||m||k)) {
        MT (board,0);
        rep (i,1,n) 
         rep (j,1,k) {
             scanf ("%d",&need[i][j]);
             board[j]-=need[i][j];
         }
        rep (i,1,m) 
         rep (j,1,k) {
             scanf ("%d",&have[i][j]);
             board[j]+=have[i][j];
         }
        rep (i,1,k) 
         rep (j,1,n) 
          rep (p,1,m) {
              scanf ("%d",&price[i][j][p]);
          }
        int flag=0;
        rep (i,1,k) {
            if (board[i]<0) {
                printf ("-1
");
               flag=1;
               break;
            }
        } 
        if (flag) continue;
        int sum=0;
        rep (i,1,k) {
            cnt=1,MT (head,-1);
            mincost=0,maxflow=0;
            rep (j,1,n) add_edge (j+50,200-1,need[j][i],0);
            rep (j,1,m) add_edge (0,j,have[j][i],0);
            rep (j,1,n) 
             rep (p,1,m) {
                 add_edge (p,j+50,inf,price[i][j][p]);
             }
             s=0,t=200-1;
             mcmf ();
             sum+=mincost;
        } 
        printf ("%d
",sum);

    }


    return 0;
}
原文地址:https://www.cnblogs.com/hhlya/p/13591900.html