zoukankan      html  css  js  c++  java
  • HDU5644 最小费用流

      这个题的大意是招募飞行员, 由于要进行n天的军演所以要招募一些飞行员, 第i天需要招募pi的飞行员, 刚开始有k个飞行员, 飞行员工作一天后有m个休假方案, 没个休假方案为工作后ti天又重新开始工作, 拿到si的薪水, 另外也可以选择招募飞行员, 从第p天开始招募, 招募需要话费Q元钱。
    考虑第i天飞行员的来源, 分为三个来源, 1:初始的k个飞行员 2:招募的飞行员 3:休假回来的飞行员

     对于第一种我们可以从源点引出一条边s - >  第一天, 容量为k, 费用为0。然后第i天向第i+1天引出一条边, 容量为inf, 费用为0

       对于第二种, 我们可以从源点引出一条边指向第i天, 容量为inf, 费用为Q

       对于第三种我们可以将地i天的点拆成两个点x, x',假设当前天的飞行员采用休假方案j后又重新开始飞行那么就连一条边 x -> (x+t[j])', 容量为inf, 费用为sj, 代码如下:

    #include <bits/stdc++.h>
    
    using namespace std;
    const int inf = 0x3f3f3f3f;
    const int maxn = 500;
    
    struct Edge {int from, to, cap, flow, cost;};
    vector<Edge> edges;
    vector<int> G[maxn];
    int inque[maxn];  //spfa
    int d[maxn];   //spfa
    int p[maxn];   //入弧编号
    int a[maxn];   //可改进量
    
    void init(int n) {
        for(int i=0; i<=n; i++) G[i].clear();
        edges.clear();
    }
    
    void add_edge(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, long long &cost) {
        memset(d, 0x3f, sizeof(d));
        memset(inque, 0, sizeof(inque));
        d[s] = 0; inque[s] = 1;
        a[s] = inf; d[s] = 0;
        queue<int> que;
        que.push(s);
        while(!que.empty()){
            int u = que.front(); que.pop();
            inque[u] = 0;
            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;
                    if(!inque[e.to]) que.push(e.to), inque[e.to]=1;
                    p[e.to] = G[u][i];
                    a[e.to] = min(a[u], e.cap-e.flow);
                }
            }
        }
        if(d[t] == inf) return false;
        flow += a[t];
        cost += (long long)a[t] * (long long)d[t];
        for(int u=t; u!=s; u=edges[p[u]].from){
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -= a[t];
        }
        return true;
    }
    
    int MCMF(int s, int t, long long &cost){
        int flow = 0; cost = 0;
        while(spfa(s, t, flow, cost));
        return flow;
    }
    
    int n, k;
    int pp[250];
    int m, P, Q;
    int s[10], t[10];
    
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            scanf("%d%d", &n, &k);
            int sumpeo = 0;
            for(int i=1; i<=n; i++) scanf("%d", &pp[i]), sumpeo += pp[i];
            scanf("%d%d%d", &m, &P, &Q);
            for(int i=0; i<m; i++) scanf("%d%d", &s[i], &t[i]);
            init(2*n+2);
            int S = 0, T = 2*n+1;
            add_edge(S, 2, k, 0);
            for(int i=1; i<=n; i++){
                add_edge(S, 2*i-1, pp[i], 0);
                //people belong to k
                if(i+1<=n) add_edge(2*i, 2*(i+1), inf, 0);
                //招募
                if(i>=P) add_edge(S, 2*i, inf, Q);
                //休假回来的人
                for(int j=0; j<m; j++)  if(i+t[j]<=n) add_edge(2*i-1, 2*(i+t[j]), inf, s[j]);
                add_edge(2*i, T, pp[i], 0);
            }
            long long cost;
            int flow = MCMF(S, T, cost);
            //printf("flow = %d
    ", flow);
            if(flow != sumpeo)
                printf("No solution
    ");
            else
                printf("%lld
    ", cost);
        }
        return 0;
    }
  • 相关阅读:
    form组件进阶_django
    form组件_django
    django的数据库ORM进阶操作
    内网安装python模块_python
    Redhat7.4安装oracle11.2.0.4版本数据库遇见的问题_oracle
    Redis基础数据类型与对象
    SpringIOC容器——ApplicationContext和BeanFactory
    AQS源码解析
    Java内存模型(一)
    面试准备笔记
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5280718.html
Copyright © 2011-2022 走看看