poj 2516 Minimum Cost 【图论-网络流-最小费用流-spfa】

来源:互联网 发布:lovelive动作数据资源 编辑:程序博客网 时间:2024/05/17 23:06
                                    Minimum Cost                    Time Limit: 4000MS      Memory Limit: 65536K

Description
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

题目大意:有n个店主对k种物品有不同需求量,现有一些供应商,每个供应商对k种物品的供应量不尽相同,先给出每个供应商运输第k(i)种物品到不同店主家的运输费用,请你计算满足所有店主的需求时的最小运费费用。

样例分析:
这里写图片描述
注:图片来自:優YoUhttp://blog.csdn.net/lyy289065406/article/details/6742534

知识点:最小费用流

解答思路:
源点->需求商->供应商->汇点
由于一种有k种商品,需要求k次最小费用流,并k次计算的结果相加

AC代码:

//算法: spfa// 使用kuangbin的模板# include <iostream># include <cstring># include <algorithm>using namespace std;# define MAXN 505# define INF 1e9 + 100struct EDGE{    int to;    int cap;  //存放容量    int flow; //存放流量    int cost; //存放费用    int next;}edge[MAXN * 10];int tot;int head[MAXN];int pre[MAXN];int que[MAXN * 100];int spkneed[MAXN][MAXN]; //记录店主对各种物品的需求量int spsup[MAXN][MAXN];   //记录所有供应商对各种物品的供应量int kinum[MAXN]; //存放所有供应商供应第i种商品的数目void Init(){    tot = 0;    memset(head, -1, sizeof(head));}void Addedge(int u, int v, int cap, int cost){    edge[tot].to = v;    edge[tot].cap = cap;    edge[tot].cost = cost;    edge[tot].flow = 0;    edge[tot].next = head[u];    head[u] = tot++;    edge[tot].to = u;    edge[tot].cap = 0;    edge[tot].cost = -cost;    edge[tot].flow = 0;    edge[tot].next = head[v];    head[v] = tot++;}bool Spfa(int s, int t){    int top = 1;    int tail = 1;    int dis[MAXN];    int vis[MAXN];    int i;    for (i = 0; i < MAXN; i++)    {        dis[i] = INF;        vis[i] = 0;        pre[i] = -1;    }    dis[s] = 0;    vis[s] = 1;    que[tail++] = s;    while (top < tail)    {        int u = que[top++];        vis[u] = 0;        for (i = head[u]; i != -1; i = edge[i].next)        {            int v = edge[i].to;            if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost)            {                dis[v] = dis[u] + edge[i].cost;                pre[v] = i;                if (!vis[v])                {                    vis[v] = 1;                    que[tail++] = v;                }            }        }    }    if (-1 == pre[t])    {        return false;    }    else    {        return true;    }}int Mincost(int s, int t){    int cost = 0;    int flow = 0;    while (Spfa(s, t))    {        int Min = INF;        int i;        for (i = pre[t]; i != -1; i = pre[edge[i^1].to])        {            if (Min > edge[i].cap - edge[i].flow)            {                Min = edge[i].cap - edge[i].flow;            }        }        for (i = pre[t]; i != -1; i = pre[edge[i^1].to])        {            edge[i].flow += Min;            edge[i^1].flow -= Min;            cost += edge[i].cost * Min;            //printf("cost =%d\n", cost);        }        flow += Min;    }    return cost;}int main(void){    int n, m, k;    while (~scanf("%d %d %d", &n, &m, &k), n && m && k)    {        int curn, curm, curk;        for (curn = 1; curn <= n; curn++)        {            for (curk = 1; curk <= k; curk++)            {                scanf("%d", &spkneed[curn][curk]);            }        }        memset(kinum, 0, sizeof(kinum));        for (curm = 1; curm <= m; curm++)        {            for (curk = 1; curk <= k; curk++)            {                scanf("%d", &spsup[curm][curk]);                kinum[curk] += spsup[curm][curk]; //用来存放所有供应提供curk种物品的总和            }        }        int result = 0;        int flag = 1;        for (curk = 1; curk <= k; curk++)        {            Init();            int s = 0; //源点            int t = n + m + 1; //汇点            int maxneedcurk = 0; //用于记录所有店主需要curk种物品的总数量            for (curn = 1; curn <= n; curn++)            {                Addedge(s, curn, spkneed[curn][curk], 0); //把所有店主与源点相连                maxneedcurk += spkneed[curn][curk];            }            if (maxneedcurk > kinum[curk]) //当需求量大于供应时,即无法完成运输任务            {                flag = 0;            }            for (curn = 1; curn <= n; curn++)            {                for (curm = 1; curm <= m; curm++)                {                    int cost;                    scanf("%d", &cost);                    Addedge(curn, curm + n, INF, cost);  //把所有供应curk中商品的供应商与所有需求curk种商品的店主连接                }            }            for (curm = 1; curm <= m; curm++)            {                Addedge(n + curm, t, spsup[curm][curk], 0); //把所有供应商与汇点相连            }            if (flag)            {                result += Mincost(s, t);            }        }        if (flag)        {            printf("%d\n", result);        }        else        {            printf("-1\n");        }    }    return 0;}
0 0
原创粉丝点击