【最小费用最大流】POJ 2516【unsolved】

妈的!!!wa死,不知道死在哪里。。。先放着

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define pii pair<int ,int>
#define bug cout<<"here!!"<<endl
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF  freopen("output.txt","w",stdout)
#define eps 1e-8
#define MIN INT_MIN
#define inf 1<<30
#define N 105
int cap[105][105];
int cost[105][105];
int pre[105];
int dis[105];
bool vis[105];
int flow[N][N];
int minc;
int min(int a,int b){return a>b?b:a;}
void spfa(int s,int t){
    queue<int> qq;
    int i,j;
    while(1){
        for(i=s;i<=t;i++){
            vis[i] = 0;
            dis[i] = inf;
        }
        dis[s] = 0;
        qq.push(s);
        vis[s] = 1;
        while(!qq.empty()){
            int u = qq.front();
            qq.pop();
            vis[u] = 0;
            for(int v=s;v<=t;v++){
                if(cap[u][v] > flow[u][v] && dis[u] + cost[u][v] < dis[v]){
                    dis[v] = dis[u] + cost[u][v];
                    pre[v] = u;
                    if(!vis[v]){
                        vis[v] = 1;
                        qq.push(v);
                    }
                }
            }
        }
        if(dis[t] == inf){
            return ;
        }
        int a = inf;
        for(int u=t;u!=s;u=pre[u]){
            a = min(a,cap[pre[u]][u] - flow[pre[u]][u]);
        }
        for(int u=t;u!=s;u=pre[u]){
            flow[pre[u]][u] += a;
            flow[u][pre[u]] -= a;
        }
        minc += dis[t]*a;
    }
    return ;
}

int needN[N][N];//人
int needM[N][N];//仓库
int nn[N];
int mm[N];
int main(){
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k) && (n+m+k)){
        int i,j;
        memset(nn,0,sizeof(nn));
        memset(mm,0,sizeof(mm));
        for(i=1;i<=n;i++){
            for(j=1;j<=k;j++){
                scanf("%d",&needN[i][j]);
                nn[j] += needN[i][j];
            }
        }
        for(i=1;i<=m;i++){
            for(j=1;j<=k;j++){
                scanf("%d",&needM[i][j]);
                mm[j] += needM[i][j];
            }
        }
        bool ok = 1;
        for(i=1;i<=k;i++){
            if(nn[i]>mm[i]){
                ok = 0;
                break;
            }
        }
        minc = 0;
        int s = 0,t = n+m+1;
        for(int kk=1;kk<=k;kk++){
            memset(cap,0,sizeof(cap));
            memset(cost,0,sizeof(cost));
            memset(flow,0,sizeof(flow));
            for(i=1;i<=n;i++){
                for(j=1;j<=m;j++){
                    scanf("%d",&cost[j][i+m]);
                    cost[i+m][j] = -cost[i+m][j];
                    cap[j][i+m] = needM[j][kk];
                }
            }
            if(!ok)continue;
            for(i=1;i<=m;i++){
                cost[s][i] = cost[i][s] = 0;
                cap[s][i] = needM[i][kk];
            }
            for(i=1;i<=n;i++){
                cost[t][i+m] = cost[i+m][t] = 0;
                cap[i+m][t] = needN[i][kk];
            }
            spfa(s,t);
        }
        if(!ok){
            puts("-1");
        } else {
            printf("%d\n",minc);
        }
    }
    return 0;
}

你可能感兴趣的:(【最小费用最大流】POJ 2516【unsolved】)