HDU 6118 度度熊的交易计划 【最小费用最大流】 (2017"百度之星"程序设计大赛

度度熊的交易计划

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


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
 
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6143 6142 6141 6140 6139 
 
Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=6118

题目大意:

  N个城市,M条公路,每个城市花费a[i]元生产1个商品,但是最多生产b[i]个,以c[i]的价格出售最多d[i]个商品,每条公路每运送1个商品花费e[i],问最大收益

题目思路:

  【最小费用最大流】

  首先很容易想到是网络流问题,城市之间连得公路容量∞,费用为e[i],

  源S到每个城市建一条容量b,费用a的边,每个城市到汇T建一条容量d费用-c的边

  然后每个城市再向汇T连一条容量为b,费用为-a的边(等于不卖),这样就满足最大流性质。

  然后跑一遍最小费用最大流即可。

  1 /****************************************************
  2 
  3     Author : Coolxxx
  4     Copyright 2017 by Coolxxx. All rights reserved.
  5     BLOG : http://blog.csdn.net/u010568270
  6 
  7 ****************************************************/
  8 #include<bits/stdc++.h>
  9 #pragma comment(linker,"/STACK:1024000000,1024000000")
 10 #define abs(a) ((a)>0?(a):(-(a)))
 11 #define lowbit(a) (a&(-a))
 12 #define sqr(a) ((a)*(a))
 13 #define mem(a,b) memset(a,b,sizeof(a))
 14 const double EPS=0.00001;
 15 const int J=10;
 16 const int MOD=1000000007;
 17 const int MAX=0x7f7f7f7f;
 18 const double PI=3.14159265358979323;
 19 const int N=504;
 20 const int M=10004;
 21 using namespace std;
 22 typedef long long LL;
 23 double anss;
 24 LL aans;
 25 int cas,cass;
 26 int n,m,lll,ans;
 27 struct xxx
 28 {
 29     int next,e,q,cost;
 30 }a[M];
 31 int last[N],d[N],hs[N];
 32 int S,T,nn;
 33 bool mark[N],K;
 34 void add(int x,int y,int z,int c)
 35 {
 36     a[++lll].next=last[x];
 37     last[x]=lll;
 38     a[lll].e=y;
 39     a[lll].q=z;
 40     a[lll].cost=c;
 41 }
 42 void build()
 43 {
 44     int i,j,k,x,y,z;
 45     S=n+1,T=n+2;
 46     nn=T;
 47     for(i=1;i<=n;i++)
 48     {
 49         scanf("%d%d%d%d",&x,&y,&z,&k);
 50         add(S,i,y,x);
 51         add(i,S,0,-x);
 52         
 53         add(i,T,y,-x);
 54         add(T,i,0,x);
 55         
 56         add(i,T,k,-z);
 57         add(T,i,0,z);
 58     }
 59     for(i=1;i<=m;i++)
 60     {
 61         scanf("%d%d%d",&x,&y,&z);
 62         
 63         add(x,y,MAX,z);
 64         add(y,x,0,-z);
 65         
 66         add(y,x,MAX,z);
 67         add(x,y,0,-z);
 68     }
 69 }
 70 bool spfa()
 71 {
 72     int qq[M+M];
 73     int i,now,head,tail;
 74     //mem(mark,0);mem(d,0x7f);
 75     for(i=1;i<=nn;i++)mark[i]=0,d[i]=MAX;
 76     head=N;tail=N;
 77     qq[N]=S;
 78     d[S]=0;mark[S]=1;
 79     while(head<=tail)
 80     {
 81         now=qq[head];
 82         head++;
 83         for(i=last[now];i;i=a[i].next)
 84         {
 85             if(a[i].q>0 && d[now]+a[i].cost<d[a[i].e])
 86             {
 87                 d[a[i].e]=d[now]+a[i].cost;
 88                 if(!mark[a[i].e])
 89                 {
 90                     mark[a[i].e]=1;
 91                     if(d[a[i].e]<d[qq[head]])
 92                         qq[--head]=a[i].e;
 93                     else
 94                         qq[++tail]=a[i].e;
 95                 }
 96             }
 97         }
 98         mark[now]=0;
 99     }
100     if(d[T]==MAX)return 0;
101     else return 1;
102 }
103 int dfs(int u,int f)
104 {
105     int v,i,tt,asp=0;
106     if(u==T)
107     {
108         K=1;
109         return f;
110     }
111     mark[u]=1;
112     for(i=last[u];i;i=a[i].next)
113     {
114         if(!mark[a[i].e] && a[i].q>0 && d[u]+a[i].cost==d[a[i].e])
115         {
116             tt=dfs(a[i].e,min(a[i].q,f-asp));
117             a[i].q-=tt;
118             a[i^1].q+=tt;
119             ans+=tt*a[i].cost;
120             asp+=tt;
121             if(asp==f)
122                 return f;
123         }
124     }
125     return asp;
126 }
127 void mincostflow()
128 {
129     while(1)
130     {
131         if(!spfa())break;
132         K=1;
133         while(K)
134         {
135             K=0;
136             mem(mark,0);
137             dfs(S,MAX);
138         }
139     }
140 }
141 int main()
142 {
143     #ifndef ONLINE_JUDGE
144     freopen("1.txt","r",stdin);
145 //    freopen("2.txt","w",stdout);
146     #endif
147     int i,j,k;
148     int x,y,z;
149 //    for(scanf("%d",&cass);cass;cass--)
150 //    init();
151 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
152     while(~scanf("%d",&n))
153     {
154         scanf("%d",&m);
155         mem(last,0);
156         lll=1;ans=0;
157         build();
158         mincostflow();
159         cout<<-ans<<endl;
160     }
161     return 0;
162 }
163 /*
164 //
165 
166 //
167 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/7389183.html