HDU 6118 度度熊的交易计划 最小费用最大流

度度熊的交易计划

Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1155 Accepted Submission(s): 426

Problem Description
度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:

喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。

由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。

同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。

由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。

据测算,每一个商品运输1公里,将会花费1元。

那么喵哈哈村最多能够实现多少盈利呢?

Input
本题包含若干组测试数据。
每组测试数据包含:
第一行两个整数n,m表示喵哈哈村由n个片区、m条街道。
接下来n行,每行四个整数a[i],b[i],c[i],d[i]表示的第i个地区,能够以a[i]的价格生产,最多生产b[i]个,以c[i]的价格出售,最多出售d[i]个。
接下来m行,每行三个整数,u[i],v[i],k[i],表示该条公路连接u[i],v[i]两个片区,距离为k[i]

可能存在重边,也可能存在自环。

满足:
1<=n<=500,
1<=m<=1000,
1<=a[i],b[i],c[i],d[i],k[i]<=1000,
1<=u[i],v[i]<=n

Output
输出最多能赚多少钱。

Sample Input
2 1
5 5 6 1
3 5 7 7
1 2 1

Sample Output
23

最小费用最大流,首先建立超级源点 s ,与超级汇点 t 。

因为生产一个商品需要花费 a[i] 元,且上限为 b[i] ,所以我们从 s 向这些点之间连一条容量为 b[i] ,费用为 -a[i] 的边。

同样的道理,出售一个商品可以赚到 c[i] 元,最多出售 d[i] 个,于是我们从这些点向 t 连一条容量为 d[i] ,费用为 c[i] 的边。

最后所有的公路也是花费,从 u 到 v 连接一条双向边,容量为 INF ,费用为 -k ,然后跑一边模板即可。

注意:图中存在自环,当我们得到两点路径长度小于 0 时应终止计算。

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int MAXM= 10010;
const int MAXN=510;
struct edge
{
    int from, to, cap, cost;
} G[MAXM];

namespace IN
{
const int inBufferSize = 1<<16;
char inBuffer[inBufferSize];
char *inHead = NULL, *inTail = NULL;
inline char Getchar()
{
    if(inHead == inTail)
        inTail=(inHead=inBuffer)+fread(inBuffer, 1, inBufferSize, stdin);
    return *inHead++;
}
}
#define getchar() IN::Getchar()
template <typename T>
inline void scan_ud(T &ret)
{
    char c = getchar();
    ret = 0;
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9')
        ret = ret * 10 + (c - '0'), c = getchar();
}
int cnt;
int head[MAXN];
int dist[MAXN];
int pre[MAXN];
int path[MAXN];
bool vis[MAXN];
int n, m;
void init()
{
    memset(head, -1, sizeof(head));
    cnt=0;
}
void add_edge(int u, int v, int cap, int cost)
{
    G[cnt].to=v;
    G[cnt].cap=cap;
    G[cnt].cost=cost;
    G[cnt].from=head[u];
    head[u]=cnt++;
    G[cnt].to=u;
    G[cnt].cap=0;
    G[cnt].cost=-cost;
    G[cnt].from=head[v];
    head[v]=cnt++;
}
int spfa(int s, int t)
{
    memset(dist, -inf, sizeof(dist));
    memset(pre, -1, sizeof(pre));
    memset(path, -1, sizeof(path));
    memset(vis, false, sizeof(vis));
    int res=dist[0];
    dist[s]=0;
    vis[s]=true;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=G[i].from)
        {
            int v=G[i].to;
            if(dist[v]<dist[u]+G[i].cost&&G[i].cap>0)
            {
                dist[v]=dist[u]+G[i].cost;
                pre[v]=u;
                path[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    return dist[t]!=res;
}
int MCMF(int s, int t)
{
    int flow=0;
    int cost=0;
    while(spfa(s, t))
    {
        int Min=inf;
        for(int i=t;i!=s&&~i;i=pre[i])
          Min=min(Min, G[path[i]].cap);
        for(int i=t;i!=s&&~i;i=pre[i])
        {
            G[path[i]].cap-=Min;
            G[path[i]^1].cap+=Min;
        }
        if(dist[t]<0)
            break;
        flow+=Min;
        cost+=Min*dist[t];
    }
    return cost;
}
int main()
{
    while(~scanf("%d %d", &n, &m))
    {
        init();
        int s=0, t=n+1;
        for(int i=1; i<=n; ++i)
        {
            int a, b, c, d;
            scan_ud(a);
            scan_ud(b);
            scan_ud(c);
            scan_ud(d);
            add_edge(s, i, b, -a);
            add_edge(i, t, d, c);
        }
        while(m--)
        {
            int u, v, k;
            scan_ud(u);
            scan_ud(v);
            scan_ud(k);
            add_edge(u, v, inf, -k);
            add_edge(v, u, inf, -k);
        }

        cout<<MCMF(s, t)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值