Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693


题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的时间,每一个核在不同一时候间处理同一个工作的花费是递增的,每一个核一次仅仅能处理一个工作,求运用k个核处理这n个工作的最小花费。


分析:

分析可知,求处理全部工作的最小花费,而每次选择怎么处理我们能够通过容量都为1的边来让网络流处理,这样就转化为最小费用最大流。


首先设一个超级源点s,连接全部的工作,流量1,花费0,然后每一个工作建一个边连接每一个工作不同一时候间处理的花费,流量为1,花费为花费,然后每一个时间段在连接汇点,流

量为k( 由于在单位1的时间里有k个核在处理k个工作 ),花费为0,然后套模板求一个从 s 到 t 的最小费用最大流。


有一个优化就是发现每一个工作不同一时候间处理的花费是递增的,那么每一个核肯定每次是选择在 n/k+1前 的时间处理,所以之后的边能够直接不建边,节省时间。(经測试发现这个题目没有这一步优化会超时)


代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=222222;
struct Edge{
    int from,to,cap,flow,cost;
};
struct MCMF{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int> G[MAXN];
    int inq[MAXN];
    int d[MAXN];
    int p[MAXN];
    int a[MAXN];
    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});
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool BellmanFord(int s,int t,int& flow,int& cost){  //最短路增光
        for(int i=0;i<=n;i++)d[i]=INF;
            CL(inq,0);
        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]=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;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){
                        Q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]==INF)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 Mincost(int s,int t){  ///求费用
        int flow=0,cost=0;
        while(BellmanFord(s,t,flow,cost));
            return cost;
    }
};

int n,m,k;
MCMF solver;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int core,work,x;
        scanf("%d%d",&core,&work);
        int tmp=work/core+1;
        solver.init(work*2+2);
        for(int i=1;i<=work;i++)
        {
            solver.AddEdge(0,i,1,0);
            for(int j=1;j<=work;j++)
            {
                scanf("%d",&x);
                if(j<=tmp)
                    solver.AddEdge(i,j+work,1,x);
            }
        }
        for (int i=1;i<=work;i++)
        {
            if (i<=tmp)
                solver.AddEdge(i+work,work*2+1,core,0);
        }
        int s=0,t=work*2+1;
        int ans=solver.Mincost(s,t);
        printf("%d\n",ans);
    }
    return 0;
}


posted @ 2014-10-27 10:17  phlsheji  阅读(273)  评论(0编辑  收藏  举报