zoukankan      html  css  js  c++  java
  • 【网络流24题】分配问题

    Description

    有n件工作要分配给n个人做。第i 个人做第j 件工作产生的效益为ij c 。试设计一个将n件工作分配给n个人做的分配方案,使产生的总效益最大。
    对于给定的n件工作和n个人,计算最优分配方案和最差分配方案。

    Input

    第1 行有1 个正整数n,表示有n件工作要分配给n 个人做。
    接下来的n 行中,每行有n 个整数ij c ,1≤i≤n,1≤j≤n,表示第i 个人做第j件工作产生的效益为ij c 。

    Output

    将计算出的最小总效益和最大总效益输出

    Sample Input

    5
    2 2 2 1 2
    2 3 1 2 4
    2 0 1 1 1
    2 3 4 3 3
    3 2 1 2 1

    Sample Output

    5
    14

    Hint

    数据范围:
    N<=100

    正解:费用流。

    第一问直接跑,第二问费用取相反数然后跑费用流。我tm还T了一次,数组开小了woc。。

    //It is made by wfj_2048~
    #include <algorithm>
    #include <iostream>
    #include <complex>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define inf (1<<29)
    #define il inline
    #define RG  
    #define ll long long
    #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    
    using namespace std;
    
    struct edge{ int nt,to,flow,cap,cost; }g[100010];
    
    int head[10010],dis[10010],f[10010],q[10010],fa[10010],p[1000010],n,S,T,flow,cost,ans1,ans2,num=1;
    
    il int gi(){
        RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
        if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
    }
    
    il void insert(RG int from,RG int to,RG int cap,RG int cost){ g[++num]=(edge){head[from],to,0,cap,cost},head[from]=num; }
    
    il int bfs(RG int S,RG int T){
        for (RG int i=1;i<=2*n+2;++i) dis[i]=inf;
        RG int h=0,t=1; q[t]=S,dis[S]=0,f[S]=inf;
        while (h<t){
        RG int x=q[++h],v;
        for (RG int i=head[x];i;i=g[i].nt){
            v=g[i].to;
            if (dis[v]>dis[x]+g[i].cost && g[i].cap>g[i].flow){
            dis[v]=dis[x]+g[i].cost,f[v]=min(f[x],g[i].cap-g[i].flow);
            q[++t]=v,fa[v]=x,p[v]=i;
            }
        }
        }
        if (dis[T]==inf) return 0; flow+=f[T],cost+=f[T]*dis[T];
        for (RG int x=T;x!=S;x=fa[x]) g[p[x]].flow+=f[T],g[p[x]^1].flow-=f[T];
        return 1;
    }
    
    il int mcmf(RG int S,RG int T){ flow=0,cost=0; while (bfs(S,T)); return cost; }
    
    il void work(){
        n=gi(),S=2*n+1,T=2*n+2; for (RG int i=1;i<=n;++i) insert(S,i,1,0),insert(i,S,0,0),insert(n+i,T,1,0),insert(T,n+i,0,0);
        for (RG int i=1;i<=n;++i) for (RG int j=1;j<=n;++j){ RG int c=gi(); insert(i,n+j,1,c),insert(n+j,i,0,-c); }
        ans1=mcmf(S,T); for (RG int i=1;i<=num;++i) g[i].flow=0,g[i].cost*=-1;
        ans2=mcmf(S,T); printf("%d
    %d
    ",ans1,-ans2); return;
    }
    
    int main(){
        File("distribution");
        work();
        return 0;
    }
  • 相关阅读:
    jmeter(46) redis
    jmeter(45) tcp/ip协议
    Codeforces Round #538 (Div. 2)D(区间DP,思维)
    Codeforces Global Round 1D(DP,思维)
    Educational Codeforces Round 57D(DP,思维)
    UPC11073(DP,思维)
    Yahoo Progamming Contest 2019D(DP,思维)
    Atcoder Beginner Contest 118D(DP,完全背包,贪心)
    Xuzhou Winter Camp 1C(模拟)
    Educational Codeforces Round 57 (Rated for Div. 2)D(动态规划)
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6429442.html
Copyright © 2011-2022 走看看