poj - 2516 最小费用最大流 拆解多源多汇

题意:有N个供应商,M个店主,K种物品。每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商Mj送第kind种货物的单位数量到店主Ni手上所需的单位花费。

问:供应是否满足需求?如果满足,最小运费是多少?

供求?花费? 看到这就想到了最小费用最大流,就直接建了一个图交了。。。Time Limit Exceeded

然后才意识到自己的图有点大。。。N*K+M*K个点、N*K*M*K条边。。。

然后又仔细看K个矩阵的时候,意识到这是要拆开来做,多源多汇的题。。。Orz

重新建图

对于每一个k

1、sp到供应商流量为k的供应量,花费为0

2、供应商到店主流量为inf,花费为对应花费

3、店主到tp流量为需求量,花费为0

求出最小费用最大流 全部加起来即为所求 判断一下最大流==需求量 相等即为满足

这样的话点数仅为N + M 边N * M

还是太弱了啊Orz

链接:poj 2516

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAX_V 1005
#define INF 0x3f3f3f3f
using namespace std;

//最小费用最大流模版.求最大费用最大流建图时把费用取负即可。
//无向边转换成有向边时需要拆分成两条有向边。即两次加边。
const int maxn = 1010;
const int maxm = 1000000 + 5000;
const int inf = 0x3f3f3f3f;

struct Edge {
    int v, cap, cost, next;
}p[maxm];

int e, sumFlow, n, m, sp, tp;
int head[maxn], dis[maxn], pre[maxn];
bool vis[maxn];
void init() {
    e = 0;
    memset(head, -1, sizeof(head));
}

void addEdge(int u, int v, int cap, int cost) {
    p[e].v = v; p[e].cap = cap; p[e].cost = cost;
    p[e].next = head[u]; head[u] = e++;
    p[e].v = u; p[e].cap = 0; p[e].cost = -cost;
    p[e].next = head[v]; head[v] = e++;
}

bool spfa(int s,int t, int n) {
    int u, v;
    queueq;
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    for(int i = 0; i <= n; i++)
        dis[i] = inf;
    vis[s] = true;
    dis[s] = 0;
    q.push(s);
    while(!q.empty()) {
        u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i != -1; i = p[i].next) {
            v = p[i].v;
            if(p[i].cap && dis[v] > dis[u] + p[i].cost) {
                dis[v] = dis[u] + p[i].cost;
                pre[v] = i;
                if(!vis[v]) {
                    q.push(v);
                    vis[v] = true;
                }
            }
        }
     }
     if(dis[t] == inf)
         return false;
     return true;
}

int MCMF(int s, int t, int n) {
    int flow = 0; // 总流量
    int minflow, mincost;
    mincost = 0;
    while(spfa(s, t, n)) {
        minflow = inf + 1;
        for(int i = pre[t]; i != -1; i = pre[p[i^1].v]) {
            if(p[i].cap < minflow) {
                minflow = p[i].cap;
            }
        }
        flow += minflow;
        for(int i = pre[t]; i != -1; i = pre[p[i^1].v]) {
            p[i].cap -= minflow;
            p[i^1].cap += minflow;
        }
        mincost += dis[t] * minflow;
    }
    sumFlow = flow; // 最大流
    return mincost;
}

int nk[maxn][maxn];
int mk[maxn][maxn];

int main ()
{
    int t, kcase = 0;
    int N, M, K;
    while(~scanf("%d %d %d", &N, &M, &K)) {
        if(!N && !M && !K) {
            break;
        }
        int sum = 0;
        for(int i = 1; i <= N; i++) {
            for(int j = 1; j <= K; j++) {
                scanf("%d", &nk[i][j]);
                sum += nk[i][j];
            }
        }
        for(int i = 1; i <= M; i++) {
            for(int j = 1; j <= K; j++) {
                scanf("%d", &mk[i][j]);
            }
        }
        int res;
        int ssumFlow = 0;
        int ans = 0;
        for(int k = 1; k <= K; k++) {
            init();
            sp = 0;
            tp = M + N + 1;
            for(int i = 1; i <= N; i++) {
                addEdge(M + i, tp, nk[i][k], 0);
            }
            for(int i = 1; i <= M; i++) {
                addEdge(sp, i, mk[i][k], 0);
            }
            for(int i = 1; i <= N; i++) {
                for(int j = 1; j <= M; j++) {
                    scanf("%d", &res);
                    addEdge(j, M + i, inf, res);
                }
            }
            ans += MCMF(0, tp, tp);

            ssumFlow += sumFlow;
        }
        if(ssumFlow != sum) {
            printf("-1\n");
        }
        else
            printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(网络流)