UVa11613 Acme Corporation(最小费用流)

 

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33452

 

【思路】

       最小费用流。

       构图:

       1 每个月建立2个点,建立st点

       2 相应连边,代价cost为正,盈利cost为负。

       跑最小费用流,即当费用大于0时停止最短路增广。即在满足相应流量的限制下不要求流量,只要求费用最小。

 

【代码】

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<vector>
 5 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 6 using namespace std;
 7 
 8 typedef long long LL ;
 9 const int maxn = 300+10;
10 const int INF = 1e9;
11 
12 struct Edge{ int u,v,cap,flow,cost;
13 };
14 
15 struct MCMF {
16     int n,m,s,t;
17     int inq[maxn],a[maxn],d[maxn],p[maxn];
18     vector<int> G[maxn];
19     vector<Edge> es;
20     
21     void init(int n) {
22         this->n=n;
23         es.clear();
24         for(int i=0;i<n;i++) G[i].clear();
25     }
26     void AddEdge(int u,int v,int cap,int cost) {
27         es.push_back((Edge){u,v,cap,0,cost});
28         es.push_back((Edge){v,u,0,0,-cost});
29         m=es.size();
30         G[u].push_back(m-2);
31         G[v].push_back(m-1);
32     }
33     
34     bool SPFA(int s,int t,int& flow,LL& cost) {
35         for(int i=0;i<n;i++) d[i]=INF;
36         memset(inq,0,sizeof(inq));
37         d[s]=0; inq[s]=1; p[s]=0; a[s]=INF; 
38         queue<int> q; q.push(s);
39         while(!q.empty()) {
40             int u=q.front(); q.pop(); inq[u]=0;
41             for(int i=0;i<G[u].size();i++) {
42                 Edge& e=es[G[u][i]];
43                 int v=e.v;
44                 if(e.cap>e.flow && d[v]>d[u]+e.cost) {
45                     d[v]=d[u]+e.cost;
46                     p[v]=G[u][i];
47                     a[v]=min(a[u],e.cap-e.flow);        //min(a[u],..)
48                     if(!inq[v]) { inq[v]=1; q.push(v);
49                     }
50                 }
51             }
52         }
53         if(d[t]>0) return false;
54         flow+=a[t] , cost+=(LL) a[t]*d[t];
55         for(int x=t; x!=s; x=es[p[x]].u) {
56             es[p[x]].flow+=a[t]; es[p[x]^1].flow-=a[t];
57         }
58         return true;
59     }
60     int Mincost(int s,int t,LL& cost) {
61         int flow=0; cost=0;
62         while(SPFA(s,t,flow,cost)) ;
63         return flow;
64     }
65 } mc;
66 
67 int M,I,m,n,p,s,E;
68 
69 int main() {
70     int T,kase=0;
71     scanf("%d",&T);
72     while(T--) {
73         scanf("%d%d",&M,&I);
74         int start=M*2,end=start+1;
75         mc.init(M*2+2);
76         FOR(i,0,M) {
77             scanf("%d%d%d%d%d",&m,&n,&p,&s,&E);
78             mc.AddEdge(start,i,n,m);
79             mc.AddEdge(M+i,end,s,-p);
80             FOR(j,0,E+1)
81                 if(i+j<M)  mc.AddEdge(i,M+i+j,INF,I*j);
82         }
83         int flow; LL cost;
84         flow=mc.Mincost(start,end,cost);
85         printf("Case %d: %lld\n",++kase,-cost);
86     }
87     return 0;
88 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值