[网络流24题] 17 运输问题(网络费用流量,最小费用最大流)

题目大意:

有m个仓库,n个商店,给出每个仓库存储货物的数量,每个商店需进货的数量,给出每个仓库向每个商店运送单位货物的价格,求满足所有商店进货需求的最小花费及最大花费;

思路分析:

①:设立一个源点s,从s向每个仓库连一条边,容量为该仓库存储的货物数量,费用为0;

②:从每个仓库向每个商店连一条边,容量为INF,当求最小花费时,费用为该单位向该商店运送单位货物的价格,当求最大花费时,费用就为该单位向该商店运送单位货物的价格的相反数;

③:设立一个汇点,从每个商店向汇点练一条边,容量该商店需进货的数量,费用为0;

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std;
const int N=210, M=200410, INF=0x3f3f3f3f;
int n, m, s, t, top, tp, min_cost;
int head[N], vis[N], dis[N], path[N], minflow[N], pre[N], ware[110], shop[110], cost[110][110];

struct Edge{
    int to,next,flow,cost;
    Edge(int _to=0,int _next=0,int _flow=0,int _cost=0):to(_to),next(_next),flow(_flow),cost(_cost){}
}edge[M];

void Addedge(int from,int to,int flow,int cost){
    edge[top] = Edge(to,head[from],flow,cost);
    head[from] = top++;
    edge[top] = Edge(from,head[to],0,-cost);
    head[to] = top++;
}

int Spfa(){
    queue<int> q;
    memset(dis, 0x3f, sizeof(dis));
    memset(minflow, 0x3f, sizeof(minflow));
    memset(path, -1, sizeof(path));
    memset(vis, 0, sizeof(vis));
    dis[s]=0; q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i = head[u]; i + 1; i = edge[i].next){
            if(edge[i].flow && dis[edge[i].to] > dis[u] + edge[i].cost){
                dis[edge[i].to] = dis[u] + edge[i].cost;
                pre[edge[i].to] = u;path[edge[i].to] = i;
                minflow[edge[i].to] = Min(minflow[u], edge[i].flow);
                if(!vis[edge[i].to]){
                    vis[edge[i].to] = 1;
                    q.push(edge[i].to);
                }
            }
        }
    }
    if(dis[t] == INF) return 0;
    min_cost += minflow[t]*dis[t];
    int u = t;
    while(u!=s){
        edge[path[u]].flow -= minflow[t];
        edge[path[u]^1].flow += minflow[t];
        u = pre[u];
    }
    return 1;
}

int main(){
    freopen("tran.in", "r", stdin);
    freopen("tran.out", "w", stdout);
    scanf("%d %d", &m, &n);
    memset(head, -1, sizeof(head));
    top = s = min_cost = 0;
    t = n+m+1;
    int va;
    for(int i = 1; i <= m; ++i){
        scanf("%d", &ware[i]);
        Addedge(s, i, ware[i], 0);
    }
    for(int i = 1; i <= n; ++i){
        scanf("%d", &shop[i]);
        Addedge(i+m, t, shop[i], 0);
    }
    for(int i = 1; i <= m; ++i){
        for(int j = 1; j <= n; ++j){
            scanf("%d", &cost[i][j]);
            Addedge(i, j+m, INF, cost[i][j]);
        }
    }
    while(Spfa());
    printf("%d\n", min_cost);
    memset(head, -1, sizeof(head));
    top = s = min_cost = 0;
    t = n+m+1;
    for(int i = 1; i <= m; ++i) Addedge(s, i, ware[i], 0);
    for(int i = 1; i <= n; ++i) Addedge(i+m, t, shop[i], 0);
    for(int i = 1; i <= m; ++i)
        for(int j =1; j <= n; ++j) Addedge(i, j+m, INF, -cost[i][j]);
    while(Spfa());
    printf("%d\n", -min_cost);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值