poj 2516(最小费用最大流)

本文详细解析了一道关于最小费用最大流的经典算法题目。通过分析题目的具体要求及限制条件,介绍了如何合理构建图模型,并分享了从初始错误尝试到最终正确实现的经验教训。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实题意很明确,最小费用最大流, 但是我这2货就建图就太二了, 我把所有的情况都弄到一个图里面。 总的点数有5000个,加上这么多的边,果断TLE。。。

后面知道第k个的情况是独立的,所以可以分成K次建图。 然后每次的点为100, 这样复杂度会小很多很多。 以后注意了,不要那么二的全部都在一起。 要坚持点边越少越好。 

 

Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 11552 Accepted: 3899

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

Source

 

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 5050
#define INF 0x3ffffff

struct node
{
    int to,next,w,c;
}edge[N*100];

int n,m,k;
int s,t;
int sn[N][N],sm[N][N];
int cnt,pre[N];
int que[N*1000];
int point[N],pedge[N];
int mxf;


void add_edge(int u,int v,int w,int c)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].c=c;
    edge[cnt].next = pre[u];
    pre[u]=cnt++;
}

int spfa()
{
    int qd=0,qf=1;
    int dis[N];
    int mark[N];
    memset(point,-1,sizeof(point));
    memset(pedge,-1,sizeof(pedge));
    for(int i=0;i<=t;i++)
    {
        dis[i]=INF;
        mark[i]=0;
    }
    dis[s]=0;
    que[0]=s;
    mark[s]=1;
    while(qf>qd)
    {
        int cur=que[qd++];
        mark[cur]=0;
        for(int p=pre[cur];p!=-1;p = edge[p].next)
        {
            int v=edge[p].to;
            int c=edge[p].c;
            int w=edge[p].w;
            if(w==0) continue;
            if( dis[v]>dis[cur]+c )
            {
                dis[v]=dis[cur]+c;
                point[v]=cur;
                pedge[v]=p;
                if(mark[v]==0)
                {
                    mark[v]=1;
                    que[qf++]=v;
                }
            }
        }
    }
    if( dis[t] == INF ) return 0;
    else return 1;
}

int fuc()
{
    int sum=0;
    int mi=INF;
    int tmp=t;
    while(tmp!=s)
    {
        int p=pedge[tmp];
        if(edge[p].w<mi) mi=edge[p].w;
        tmp=point[tmp];
    }
    tmp=t;
    mxf += mi;
    while(tmp!=s)
    {
        int p=pedge[tmp];
        edge[p].w -= mi;
        edge[p^1].w += mi;
        sum += mi*edge[p].c;
        tmp=point[tmp];
    }
    return sum;
}

int main()
{
    int ans;
    while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k))
    {
        int sum=0;
        mxf=0;
        ans=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=k;j++)
            {
                scanf("%d",&sn[i][j]);
                ans += sn[i][j];
            }

        for(int i=1;i<=m;i++)
            for(int j=1;j<=k;j++)
                scanf("%d",&sm[i][j]);
    
        for(int i=1;i<=k;i++)
        {
            cnt=0;
            memset(pre,-1,sizeof(pre));
            s=0;
            t=n+m+1;
            for(int j=1;j<=n;j++)
            {
                add_edge(s,j,sn[j][i],0);
                add_edge(j,s,0,0);
            }
            for(int j=1;j<=m;j++)
            {
                add_edge(n+j,t,sm[j][i],0);
                add_edge(t,n+j,0,0);
            }
            for(int j=1;j<=n;j++)
                for(int i1=1;i1<=m;i1++)
                {
                    int tmp;
                    scanf("%d",&tmp);
                    add_edge(j,n+i1,INF,tmp);
                    add_edge(n+i1,j,0,-tmp);
                }
            while(spfa())
            {
                sum += fuc();
            }
        }
        if( mxf != ans ) printf("-1\n");
        else printf("%d\n",sum);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/03/10/2952374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值