POJ2516

来源:互联网 发布:linux怎么学 编辑:程序博客网 时间:2024/05/21 07:03

新手操作,同样借助了上次大神的模板,接下来的事情就很容易了

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int MAXNODE = 5005;const int MAXEDGE = 500005;typedef int Type;const Type INF = 0x3f3f3f3f;struct Edge {    int u, v;    Type cap, flow, cost;    Edge() {}    Edge(int u, int v, Type cap, Type flow, Type cost) {        this->u = u;        this->v = v;        this->cap = cap;        this->flow = flow;        this->cost = cost;    }};struct MCFC {    int n, m, s, t;    Edge edges[MAXEDGE];    int first[MAXNODE];    int next[MAXEDGE];    int inq[MAXNODE];    Type d[MAXNODE];    int p[MAXNODE];    Type a[MAXNODE];    void init(int n) {        this->n = n;        memset(first, -1, sizeof(first));        m = 0;    }    void add_Edge(int u, int v, Type cap, Type cost) {        edges[m] = Edge(u, v, cap, 0, cost);        next[m] = first[u];        first[u] = m++;        edges[m] = Edge(v, u, 0, 0, -cost);        next[m] = first[v];        first[v] = m++;    }    bool bellmanford(int s, int t, Type &flow, Type &cost) {        for (int i = 0; i < n; i++) d[i] = INF;        memset(inq, false, sizeof(inq));        d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;        queue<int> Q;        Q.push(s);        while (!Q.empty()) {            int u = Q.front(); Q.pop();            inq[u] = false;            for (int i = first[u]; i != -1; i = next[i]) {                Edge& e = edges[i];                if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {                    d[e.v] = d[u] + e.cost;                    p[e.v] = i;                    a[e.v] = min(a[u], e.cap - e.flow);                    if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}                }            }        }        if (d[t] == INF) return false;        flow += a[t];        cost += d[t] * a[t];        int u = t;        while (u != s) {            edges[p[u]].flow += a[t];            edges[p[u]^1].flow -= a[t];            u = edges[p[u]].u;        }        return true;    }    Type Mincost(int s, int t, Type sum) {        Type flow = 0, cost = 0;        while (bellmanford(s, t, flow, cost));        if (sum != flow) cost = -1;        return cost;    }} gao;int N,M,K;int a[51][51],b[51][51];//表示供应商和店主的。int cost[51][51];void solve(){    int flag=0,tot=0;    int sum;    for(int k=1;k<=K;k++){        gao.init(200);        sum=0;        for(int i=1;i<=M;i++)            gao.add_Edge(0,i,a[i][k],0);        for(int i=1;i<=N;i++){            for(int j=1;j<=M;j++){                scanf("%d",&cost[j][i]);                gao.add_Edge(j,M+i,b[i][k],cost[j][i]);                gao.add_Edge(M+i,j,0,-cost[j][i]);            }            sum+=b[i][k];            gao.add_Edge(M+i,M+N+1,b[i][k],0);        }        if(flag)continue;        int ans=gao.Mincost(0,N+M+1,sum);        if(ans==-1){            flag=1;        }        tot+=ans;    }    if(flag)printf("-1\n");    else printf("%d\n",tot);}int main(){    while(scanf("%d%d%d",&N,&M,&K)&&N&&M&&K){        for(int i=1;i<=N;i++){            for(int j=1;j<=K;j++){                scanf("%d",&b[i][j]);            }        }        for(int i=1;i<=M;i++){            for(int j=1;j<=K;j++){                scanf("%d",&a[i][j]);            }        }        solve();    }    return 0;}


0 0
原创粉丝点击