hdu 6118 度度熊的交易计划

来源:互联网 发布:mac 10.12.6 编辑:程序博客网 时间:2024/06/06 04:30

度度熊的交易计划

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


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 15 5 6 13 5 7 71 2 1
 

Sample Output
23

建立源点s,汇点t,对于每一个片区i,连接s到i,容量为b[i],费用为a[i],连接i到t,容量为d[i],费用为c[i],最后如果两个片区i,j之间如果有一条路距离为k,则连接i -> j,j -> i容量为INF,费用为k的边,增广的时候,如果费用为正就break,最后得到的-cost就是最大利润。

#include <cstdio>#include <vector>#include <queue>#include <cstring>using namespace std;#define N 1000#define INF 100000000struct Edge{    int from,to,cap,flow,cost;    Edge(int u,int v,int ca,int f,int co):from(u),to(v),cap(ca),flow(f),cost(co){};};struct MCMF{    int n,m,s,t;    vector<Edge> edges;    vector<int> G[N];    int inq[N];//是否在队列中    int d[N];//距离    int p[N];//上一条弧    int a[N];//可改进量    void init(int n)//初始化    {        this->n=n;        for(int i=0;i<n;i++)            G[i].clear();        edges.clear();    }    void addedge(int from,int to,int cap,int cost)//加边    {        edges.push_back(Edge(from,to,cap,0,cost));        edges.push_back(Edge(to,from,0,0,-cost));        int m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool SPFA(int s,int t,int &flow,int &cost)//寻找最小费用的增广路,使用引用同时修改原flow,cost    {        for(int i=0;i<n;i++)            d[i]=INF;        memset(inq,0,sizeof(inq));        d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;        queue<int> Q;        Q.push(s);        while(!Q.empty())        {            int u=Q.front();            Q.pop();            inq[u]--;            for(int i=0;i<G[u].size();i++)            {                Edge& e=edges[G[u][i]];                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)//满足可增广且可变短                {                    d[e.to]=d[u]+e.cost;                    p[e.to]=G[u][i];                    a[e.to]=min(a[u],e.cap-e.flow);                    if(!inq[e.to])                    {                        inq[e.to]++;                        Q.push(e.to);                    }                }            }        }        //if(d[t]==INF) return false;//汇点不可达则退出        if (d[t] > 0) return false;        flow+=a[t];        cost+=d[t]*a[t];        int u=t;        while(u!=s)//更新正向边和反向边        {            edges[p[u]].flow+=a[t];            edges[p[u]^1].flow-=a[t];            u=edges[p[u]].from;        }        return true;    }    int MincotMaxflow(int s,int t)    {        int flow=0,cost=0;        while(SPFA(s,t,flow,cost));        return cost;    }};int main(){    int n, m;    while(scanf("%d%d",&n,&m)!=EOF)    {        MCMF sol;        int  s=0;        int  t=n+1;        sol.init(t + 1);        for (int i = 1; i <= n; i++)        {            int aa, bb, cc, dd;            scanf("%d%d%d%d", &aa, &bb, &cc, &dd);            sol.addedge(s, i, bb, aa);            sol.addedge(i, t, dd, -cc);        }        for (int i = 0; i < m; i++)        {            int u, v, k;            scanf("%d%d%d", &u, &v, &k);            sol.addedge(u, v, INF, k);            sol.addedge(v, u, INF, k);        }        printf("%d\n",-sol.MincotMaxflow(s, t));    }    return 0;}




阅读全文
0 0
原创粉丝点击