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

来源:互联网 发布:海马苹果助手for mac 编辑:程序博客网 时间:2024/06/05 10:52

题意:有k种物品,m个供应商,n个收购商。每个供应商和收购商都需要一些种类的物品若干。每个供应商与每个收购商之间的对于不同物品的运费是不同的。求满足收购商要求的情况下,最小运费。

思路:对于K种物品进行k次最小费用最大流。建图顺序为s-供应商-商店-t。

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<ctime>#define eps 1e-6#define LL long long#define pii pair<int, int>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;//const int MAXN = 5000000 + 5;//const int INF = 0x3f3f3f3f;//最小费用最大流const int MAXN=200;const int INF=0x3fffffff;int cap[MAXN][MAXN];//容量,没有边为0int flow[MAXN][MAXN];//耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数int cost[MAXN][MAXN];int n;//顶点数目1~nint f;//最大流int c;//最小费用int s, t;//源点和汇点bool vis[MAXN];//在队列标志int pre[MAXN];int dist[MAXN];//s-t路径最小耗费queue<int> q;void init(){    memset(cost, 0, sizeof(cost));    memset(cap, 0, sizeof(cap));}bool SPFA(){    while(!q.empty()) q.pop();    memset(vis, 0, sizeof(vis));    for(int i = 1; i <= n; i++) dist[i] = INF;    q.push(s);    vis[s] = true;    dist[s] = 0;    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u]=false;        for(int v = 1; v <= n;v++)        {            if(cap[u][v]>flow[u][v] && dist[v]>dist[u]+cost[u][v])            {                dist[v] = dist[u]+cost[u][v];                pre[v]=u;                if(!vis[v])                {                    vis[v]=true;                    q.push(v);                }            }        }    }    if(dist[t] >= INF) return false;    return true;}void minCostMaxflow(){    memset(flow,0,sizeof(flow));    c=f=0;    while(SPFA())    {        int Min=INF;        for(int u=t;u!=s;u=pre[u])           Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]);        for(int u=t;u!=s;u=pre[u])        {            flow[pre[u]][u]+=Min;            flow[u][pre[u]]-=Min;        }        c+=dist[t]*Min;        f+=Min;    }}int shop, sup, good;int goodShopNeed[60][60], supplyStore[60][60], unitCost[60][60][60];int main(){    //freopen("input.txt", "r", stdin);while(cin>>shop>>sup>>good && shop)    {        init();        for(int i = 1; i <= shop; i++)            for(int j = 1; j <= good; j++)                scanf("%d", &goodShopNeed[i][j]);        for(int i = 1; i <= sup; i++)            for(int j = 1; j <= good; j++)                scanf("%d", &supplyStore[i][j]);        for(int i = 1; i <= good; i++)            for(int j = 1; j <= shop; j++)                for(int k = 1; k <= sup; k++)                    scanf("%d", &unitCost[i][j][k]);        int res = 0;        n = 110, s = 109, t = 110;        for(int i = 1; i <= good; i++)        {            int sumv = 0;            for(int j = 1; j <= shop; j++) sumv += goodShopNeed[j][i];            for(int j = 1; j <= sup; j++)                cap[s][j] = supplyStore[j][i];            for(int j = 1; j <= shop; j++)                cap[50+j][t] = goodShopNeed[j][i];            for(int j = 1; j <= sup; j++)                for(int k = 1; k <= shop; k++)                {                    cap[j][50+k] = supplyStore[j][i];                    cost[j][50+k] = unitCost[i][k][j];                    cost[50+k][j] = -cost[j][50+k];                }            minCostMaxflow();            if(f < sumv)            {                res = -1;                break;            }            res += c;        }        cout << res << endl;}    return 0;}


0 0
原创粉丝点击