• 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
    ",++kase,-cost);
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    table标签去除默认边框
    自定义矢量图
    ClickJacking(点击劫持)
    css 字体不撑开默认块级元素问题
    meate 标签使用介绍
    intellij IDEA15 设置背景颜色
    JS 浮点数运算丢失精度解决方案
    IDEA 滚动条跳动问题
    JS
    异步变同步
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5066550.html
Copyright © 2020-2023  润新知