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

来源:互联网 发布:c语言次方怎么写 编辑:程序博客网 时间:2024/05/18 02:24


Minimum Cost
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 17336 Accepted: 6095

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 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

这道题 又是mmp,  把k和m 输入输反了, 恰巧  示例 是1 3 3   所以,  一直wa  后来 变成LTE  


谢谢前人 给出的 数据 卡TLE;


题意 : 给 n 个地点, 需要k 个物品,  n行输入, 代表n个地点需要 k类物品数量,   m行输入 代表 m个地点提供 k类物品


遇到问题:   是否要拆点?   拆点的话 会不会超时?


如果拆点的话 :



这么多的图  访问起来 一定会TLE


我们发现  这些k 类物品 是独立的 

所以 可以这样操作 ,  进行k次spfa 操作:


这样的话 就省时




附上  前人给的 数据 检测TLE:


#include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#include <math.h>#include <cstring>#include <string>#include <queue>#include <stack>#include <stdlib.h>#include <list>#include <map>#include <set>#include <bitset>#include <vector>#define mem(a,b) memset(a,b,sizeof(a))#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define FIN      freopen("input.txt","r",stdin)#define FOUT     freopen("output.txt","w",stdout)#define S1(n)    scanf("%d",&n)#define SL1(n)   scanf("%I64d",&n)#define S2(n,m)  scanf("%d%d",&n,&m)#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)#define Pr(n)     printf("%d\n",n)using namespace std;typedef long long ll;const int INF=0x3f3f3f3f;const double esp=1e-6;const int maxn=25000;const int MOD=1e9+7;const int mod=1e9+7;int dir[5][2]={0,1,0,-1,1,0,-1,0};const int N=200;struct node{    int u,v,w,c,next;}edge[maxn];int head[maxn],start,END,cnt,sum;int dis[maxn],pre[maxn],vis[maxn];int need[N][N],suply[N][N];int n_eed[N],su_ply[N];int cost[N][N][N];void init(){    cnt=0;    mem(head,-1);}void add(int u,int v,int c,int w)//u ??,v ?? c ?? w ??{    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].c=c;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;    // ???, ??? cost = -cost    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].w=-w;    edge[cnt].c=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int SPFA(){    queue<int>Q;    mem(pre,-1);    mem(vis,0);    mem(dis,INF);    dis[start]=0;    vis[start]=1;    Q.push(start);    while(!Q.empty())    {        int t=Q.front();        Q.pop();        vis[t]=0;        for(int i=head[t];i!=-1;i=edge[i].next)//        {            int t1= edge[i].v;//????            int t2= edge[i].w;// ??            if(edge[i].c&&dis[t1]>dis[t]+t2)// ???            {                dis[t1]=dis[t]+t2;                pre[t1]=i;                if(!vis[t1])                {                    Q.push(t1);                    vis[t1]=1;                }            }        }    }    if(dis[END]==INF)return 0;    return 1;}int MCMF(){    int minflow=0;    int mincost=0;    int ans=0;    while(SPFA())    {        minflow=INF;        int cost=0;        for(int i=pre[END];i!=-1;i=pre[edge[i].u])        {            minflow=min(minflow,edge[i].c);        }        for(int i=pre[END];i!=-1;i=pre[edge[i].u])        {            edge[i].c-=minflow;            edge[i^1].c+=minflow;            cost+=minflow*edge[i].w;        }        ans+=cost;        mincost+=dis[END];    }    return ans;}void clear(){mem(need,0);    mem(suply,0);    mem(cost,0);    mem(n_eed,0);    mem(su_ply,0);}int n,k,m;int main(){while(~scanf("%d %d %d",&n,&m,&k)){if(!n&&!k&&!m)break;init();clear();for(int i=1;i<=n;i++){for(int j=1;j<=k;j++){S1(need[i][j]);n_eed[j]+=need[i][j];}}for(int i=1;i<=m;i++){int x;for(int j=1;j<=k;j++){S1(suply[i][j]);su_ply[j]+=suply[i][j];}}        for(int hh=1;hh<=k;hh++)for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)S1(cost[hh][i][j]);int flag=0;for(int i=1;i<=k;i++){if(su_ply[i]<n_eed[i])flag=1;}int ans=0;if(flag){printf("-1\n");continue;}start=0,END=n*m+1;for(int hh=1;hh<=k;hh++){init();for(int j=1;j<=n;j++)// 汇点add(start,j,need[j][hh],0);for(int j=1;j<=m;j++)// 源点add(j+n,END,suply[j][hh],0);for(int i=1;i<=n;i++)// ±ßfor(int j=1;j<=m;j++)add(i,j+n,INF,cost[hh][i][j]);ans+=MCMF();}cout<<ans<<endl;}return 0;}



数据:


3 16 283 0 2 2 3 0 2 0 2 1 2 1 3 3 0 3 1 2 0 2 1 2 1 2 0 3 0 2 2 3 2 0 2 0 1 2 0 3 1 3 1 1 3 2 2 0 2 2 0 1 0 3 2 3 2 3 3 0 0 0 2 2 3 1 3 1 1 0 0 3 2 0 0 1 3 3 2 0 3 1 1 2 1 1 3 1 1 2 0 0 2 2 0 1 0 0 3 3 1 0 3 2 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75 37 66 30 62 92 82 61 67 97 80 10 50 7 40 54 58 75 74 84 21 96 97 7 88 31 42 63 96 60 54 51 70 89 11 35 91 12 87 65 19 59 9 78 83 69 17 1 67 76 10 15 70 65 44 45 74 90 81 84 22 6 84 29 14 97 2 70 19 70 88 62 52 92 78 60 66 28 55 16 22 74 50 40 11 22 14 30 67 80 70 31 41 63 20 75 91 66 90 53 64 79 16 39 86 12 18 47 87 96 93 50 46 69 69 8 76 53 35 52 73 60 11 78 1 59 52 94 68 90 78 64 69 91 93 87 15 18 1 96 58 18 70 65 39 47 92 27 19 72 34 10 69 71 46 62 71 57 65 3 74 45 10 38 97 87 49 14 98 95 45 22 10 72 24 27 67 74 86 62 11 32 31 13 5 41 72 78 47 53 47 21 17 89 77 72 36 88 1 29 72 15 20 96 94 74 83 16 73 5 69 41 52 17 46 78 3 79 30 76 42 80 74 82 71 71 22 23 89 44 65 57 47 30 39 54 89 74 81 18 69 75 4 47 59 47 96 33 53 85 6 73 92 4 28 30 85 19 29 4 91 43 58 31 97 77 61 87 91 52 40 30 38 8 98 29 72 75 92 16 23 37 34 38 55 23 80 15 79 45 32 91 88 6 91 89 77 81 95 47 19 28 85 59 38 10 12 87 54 93 45 70 49 99 95 3 14 72 5 98 47 49 14 19 57 88 72 85 7 46 97 47 14 50 36 20 95 68 48 86 88 20 60 54 38 23 19 60 67 22 66 51 53 14 97 78 90 81 6 78 82 65 67 55 38 8 8 39 13 61 56 51 83 16 43 30 73 38 78 60 41 74 34 50 31 6 60 25 7 8 92 38 70 14 53 5 78 21 17 49 60 46 34 63 62 70 90 73 6 64 96 11 79 68 67 87 89 76 22 68 44 27 32 43 10 51 51 81 39 43 70 94 82 67 27 63 78 9 4 67 59 5 40 82 51 73 25 61 11 61 51 81 62 34 21 81 9 21 2 36 51 65 78 29 94 52 26 53 3 94 73 7 30 44 28 4 47 98 64 76 43 80 61 94 46 53 51 50 50 54 96 76 63 10 42 13 84 14 69 93 27 20 92 90 87 54 19 62 75 82 36 54 11 39 15 97 97 47 12 58 89 61 69 71 49 35 65 21 12 47 42 4 67 15 47 32 70 90 25 6 71 4 90 18 10 24 20 42 43 14 88 21 50 41 3 46 1 33 46 31 6 35 75 64 62 42 23 52 91 90 1 39 90 67 74 6 9 12 32 35 32 82 65 63 33 75 27 87 36 32 76 37 73 42 48 13 69 99 2 83 33 81 48 32 10 21 32 61 44 57 54 44 35 19 22 23 5 31 19 41 93 90 50 38 87 18 86 47 70 21 61 61 51 70 21 5 26 41 1 39 30 4 33 98 42 22 35 40 8 56 2 13 32 6 55 42 94 97 63 25 68 70 76 40 74 49 12 45 63 40 20 5 45 75 67 85 66 26 77 73 55 89 44 65 33 7 77 68 15 96 97 63 86 51 22 74 99 95 66 98 61 65 22 31 47 86 98 50 46 98 22 82 34 47 65 24 2 59 98 45 38 23 55 38 72 97 63 33 1 70 75 65 86 99 39 1 63 30 18 1 36 41 34 41 98 33 27 71 61 61 98 11 26 60 50 85 94 78 68 93 19 22 75 10 23 57 97 6 90 84 60 99 19 93 78 33 79 36 41 50 59 44 52 12 16 80 41 99 42 98 99 44 69 76 50 54 81 89 22 47 99 16 9 72 93 25 17 47 78 29 66 62 66 75 34 5 12 40 36 34 30 98 15 79 40 52 32 80 91 79 33 37 33 71 94 5 82 40 8 85 66 68 7 69 73 59 12 53 91 80 89 4 89 47 74 5 87 47 98 30 6 47 51 95 4 83 95 37 33 81 5 58 89 95 12 91 92 31 55 33 8 26 99 81 77 55 29 35 45 44 74 2 95 45 90 50 99 70 35 73 9 21 60 83 61 36 82 9 60 59 24 30 78 84 18 99 72 62 29 94 13 10 32 56 58 62 61 43 77 31 84 77 3 70 23 91 73 97 62 62 30 51 81 37 2 50 34 50 72 44 23 17 65 99 88 12 13 20 75 21 67 52 61 68 23 69 39 21 85 78 44 47 83 43 84 56 40 2 92 83 1 12 52 45 50 46 12 48 96 73 37 41 70 7 30 28 2 98 50 18 29 54 66 25 20 12 85 2 33 2 81 10 99 11 61 32 83 74 12 3 73 14 49 73 61 44 3 95 92 67 96 41 8 90 16 12 62 64 38 93 89 94 35 1 35 19 85 39 57 85 49 72 24 69 10 90 84 7 38 72 75 24 14 55 96 32 24 84 54 42 89 64 89 13 33 83 94 49 60 36 32 71 30 14 54 95 17 81 57 16 40 16 45 48 34 37 45 12 71 80 61 23 82 54 88 72 8 48 86 51 16 6 41 6 6 44 90 88 62 40 69 81 62 46 22 55 89 72 73 46 82 93 73 17 60 14 10 72 35 43 42 7 82 36 90 47 78 37 85 76 39 70 80 93 4 38 49 92 5 53 70 36 34 81 96 69 61 2 50 96 34 74 13 31 85 27 58 38 93 28 5 21 31 87 25 29 10 31 90 48 6 86 2 65 44 56 67 62 22 86 80 15 76 9 75 75 44 85 92 6 46 12 44 20 23 16 7 48 74 21 30 36 12 83 2 4 78 92 36 7 73 47 40 78 77 74 25 26 81 32 17 60 74 40 18 5 96 86 3 96 35 63 76 11 39 67 77 7 54 24 79 54 8 4 371 3 3 0 1 0 0 1 1 0 0 0 2 0 3 2 2 3 0 2 1 1 0 1 1 3 1 0 2 1 1 1 3 0 2 3 1 1 3 3 3 3 2 3 3 1 3 0 0 0 2 1 1 1 0 0 1 2 3 0 3 1 3 2 3 3 0 0 1 3 1 1 2 1 2 2 2 0 1 0 1 1 2 3 0 3 0 2 1 0 3 1 1 1 3 0 2 1 1 0 1 2 0 2 2 1 3 2 1 3 2 2 3 3 1 0 2 2 0 1 2 0 3 3 1 1 1 3 3 0 0 3 1 3 3 0 2 3 1 3 3 1 0 1 1 1 3 3 1 2 0 3 3 3 0 1 2 0 2 1 3 3 3 1 3 0 3 1 2 0 3 1 2 2 0 3 1 0 1 2 2 3 0 2 1 3 3 0 1 2 0 0 1 3 1 2 2 3 2 2 0 3 3 0 3 3 3 2 0 2 1 3 3 2 0 0 3 2 2 0 1 3 0 2 2 3 2 1 0 0 1 3 1 0 0 3 1 3 3 3 1 1 1 3 0 2 0 1 2 0 3 1 3 0 3 1 1 3 2 0 1 2 0 0 0 3 3 3 0 2 2 2 0 1 3 1 1 0 1 3 2 3 0 3 2 0 0 1 3 3 1 1 3 1 3 0 3 1 2 0 2 1 2 3 3 0 1 3 1 2 1 0 2 1 3 1 0 3 2 0 0 0 0 1 3 1 3 1 2 1 2 2 1 3 1 1 0 0 2 3 0 2 2 2 3 1 3 1 2 2 3 0 3 1 1 3 0 3 2 1 2 3 3 1 1 0 1 2 3 0 2 2 3 0 2 3 0 0 0 1 2 3 2 3 1 3 1 1 3 2 2 1 3 0 1 1 3 1 0 0 1 1 0 0 2 2 3 0 2 1 2 2 0 1 2 3 1 0 0 1 3 2 1 1 0 3 1 1 3 1 1 0 2 2 2 1 0 2 3 0 1 0 0 3 35 57 81 65 26 23 1 79 6 2 48 84 70 11 18 97 16 50 74 93 76 1 82 88 2 67 32 77 89 63 2 1 18 7 18 78 71 38 49 13 5 2 76 49 48 27 4 38 94 48 40 88 73 73 12 9 51 79 34 82 50 19 10 45 55 18 92 12 72 59 57 21 21 24 29 58 21 81 27 7 54 23 41 97 77 91 28 10 39 33 42 80 95 25 62 94 79 68 50 95 1 50 19 95 56 30 96 27 15 59 13 11 41 96 77 90 48 37 55 92 37 88 48 73 99 36 58 18 58 29 49 39 10 16 87 49 49 80 72 42 48 87 42 82 43 92 60 11 38 42 76 81 50 6 52 33 18 58 61 6 93 69 55 20 42 9 78 15 74 32 63 63 18 47 55 70 78 27 12 71 77 82 36 20 20 38 64 5 91 43 90 70 22 57 40 88 24 32 63 49 37 38 82 21 8 62 52 51 98 16 60 58 82 81 36 75 19 36 4 4 46 87 28 47 14 57 91 65 60 32 76 30 80 49 56 16 85 5 94 20 93 72 44 30 56 12 40 37 56 51 1 20 91 39 11 92 73 28 2 45 48 4 7 55 79 11 17 23 4 93 1 93 85 4 12 16 83 90 67 15 75 27 91 12 23 87 40 86 41 27 74 93 64 87 8 50 45 68 9 89 97 93 48 13 28 17 24 18 35 83 1 42 54 8 27 14 17 72 72 28 89 10 22 48 70 72 89 54 21 62 81 34 90 17 75 38 15 13 43 9 64 99 78 71 4 31 60 29 39 81 26 39 26 26 65 19 15 92 21 35 86 31 97 43 85 24 79 53 68 92 50 35 12 1 2 81 71 34 59 31 35 45 76 65 93 24 38 86 27 31 32 78 56 9 30 82 71 20 19 63 51 51 29 14 98 88 30 68 36 50 2 70 38 43 62 72 70 49 77 4 23 15 12 95 77 26 58 83 25 59 7 95 54 83 73 33 34 20 80 52 14 9 69 65 63 3 77 48 65 42 36 96 1 22 8 15 32 13 71 52 4 39 19 97 9 77 91 54 9 89 29 77 55 36 59 8 11 97 78 2 26 42 71 62 48 73 27 91 37 94 70 66 49 8 92 41 76 22 94 11 2 45 16 5 42 57 73 62 66 46 78 64 30 83 54 67 32 38 36 98 42 90 61 74 89 60 19 20 52 1 7 8 69 80 37 22 93 91 27 61 99 17 51 92 92 1 61 56 3 35 63 33 34 23 63 98 35 35 53 47 69 94 61 22 61 34 47 34 62 57 42 15 64 24 31 59 62 21 80 38 2 24 92 13 31 7 4 31 83 20 7 58 94 80 1 99 3 93 61 72 34 68 50 99 97 27 9 62 23 71 8 98 47 16 67 78 89 54 20 37 14 50 54 15 26 32 74 63 11 90 56 88 32 36 91 67 23 35 38 21 91 74 51 94 47 69 94 93 96 75 57 54 90 6 25 93 1 22 46 4 82 7 62 54 10 68 19 40 46 2 30 77 19 50 24 53 92 87 37 40 74 77 71 88 64 15 11 97 84 18 84 87 99 69 76 65 58 71 91 15 58 28 54 93 91 14 53 76 56 20 16 26 80 61 27 25 66 52 62 51 81 85 69 49 31 52 48 38 2 44 79 55 35 64 42 53 30 81 72 33 98 64 16 77 8 30 97 12 15 41 89 1 32 87 89 94 26 75 92 12 99 87 60 2 38 3 80 64 90 62 25 70 62 66 77 45 81 79 67 74 14 49 60 14 5 80 44 8 22 9 79 47 44 97 25 62 9 88 22 56 59 63 64 2 76 15 4 98 60 4 30 11 46 90 4 77 9 21 41 37 38 66 40 25 63 65 67 21 78 10 72 77 63 89 54 16 53 52 52 34 41 94 86 87 87 29 9 7 82 63 96 7 79 9 64 52 19 80 97 31 60 99 99 3 58 41 64 72 87 58 45 66 51 38 50 44 88 74 21 32 3 55 38 12 34 45 2 54 7 31 30 19 49 44 31 25 37 16 76 2 61 69 62 30 56 20 40 93 91 81 48 10 78 43 2 21 76 31 62 78 59 96 92 81 29 71 51 58 26 10 82 6 40 62 39 87 52 22 89 18 20 54 56 17 53 70 30 72 61 67 13 94 39 59 18 89 64 57 8 44 88 13 80 62 75 61 76 19 72 22 34 74 82 41 69 52 6 78 81 78 34 6 81 95 46 96 52 82 7 27 6 40 17 15 81 18 98 42 31 25 82 94 27 89 71 40 73 36 50 4 18 19 20 39 56 95 16 80 48 74 92 49 9 30 43 2 4 89 30 23 18 32 83 15 24 62 63 95 13 12 31 78 14 51 29 14 82 28 14 82 70 53 38 41 41 14 30 53 74 70 72 68 16 86 12 48 95 65 21 96 89 43 31 35 51 1 57 16 67 53 45 70 90 72 49 67 85 21 77 52 32 20 12 93 59 56 36 61 17 40 88 80 68 18 65 99 22 66 82 84 74 48 44 9 91 62 13 37 84 96 18 71 23 76 20 57 43 33 60 18 76 3 97 34 70 43 84 99 28 20 70 2 30 15 85 56 64 24 3 10 40 11 20 86 4 43 54 67 21 28 24 78 67 39 63 84 76 49 73 52 80 2 67 98 16 76 71 74 8 31 74 89 15 52 0 0 0data1243-1

123

原创粉丝点击