分配问题 二分最佳匹配问题

来源:互联网 发布:检查ip端口是否打开 编辑:程序博客网 时间:2024/06/07 03:03

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

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

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

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

5
14

题目可以在这里提交:http://wikioi.com/homework/23/

分析:二分最佳匹配问题,源点向每个任务建边,权值为1,费用为0,每个人向汇点T建边,权值为1,费用为0,每个任务向每个人建边,权值为1,费用为mp[i][j],跑一边最大费用最大流即可。

代码:

//O(Kn^2m)//如果要求最大费用的话 只需在加边的时候加-的边  输出时输出-ans即可#pragma comment(linker,"/STACK:102400000,102400000")#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <queue>#include <stack>#include <map>#include <set>using namespace std;typedef long long ll;   //记得必要的时候改成无符号const int maxn=505;const int maxm=1000005;const int INF=1000000000;struct EdgeNode{    int from;    int to;    int flow;    int cost;    int next;}edge[maxm];int head[maxn],cnt;void add(int x,int y,int z,int c){    edge[cnt].from=x;edge[cnt].to=y;edge[cnt].flow=z;edge[cnt].cost=c;edge[cnt].next=head[x];head[x]=cnt++;    edge[cnt].from=y;edge[cnt].to=x;edge[cnt].flow=0;edge[cnt].cost=-c;edge[cnt].next=head[y];head[y]=cnt++;}void init(){    cnt=0;    memset(head,-1,sizeof(head));}int S,T,n,m;int d[maxn],in[maxn],pre[maxn];queue<int>Q;bool spfa(int S,int T,int n){    int u,v,f,c;    while(!Q.empty())Q.pop();    memset(in,0,sizeof(in));    for(int i=0;i<=n;i++)d[i]=INF;    d[S]=0;    Q.push(S);    while(!Q.empty())    {        u=Q.front(); Q.pop(); in[u]=0;        for(int i=head[u];i!=-1;i=edge[i].next){            v=edge[i].to; f=edge[i].flow; c=edge[i].cost;            if(f&&d[u]+c<d[v]){                d[v]=d[u]+c; pre[v]=i;                if(!in[v]){                    in[v]=1;                    Q.push(v);                }            }        }    }    if(d[T]==INF)return false;    return true;}int MCMF(int S,int T,int n){    int u;    int max_flow=0;    int min_cost=0;    while(spfa(S,T,n))    {        int flow=INF;        u=T;        while(u!=S){            flow=min(flow,edge[pre[u]].flow);            u=edge[pre[u]].from;        }        u=T; max_flow+=flow; min_cost+=d[T]*flow;        while(u!=S){            edge[pre[u]].flow-=flow;            edge[pre[u]^1].flow+=flow;            u=edge[pre[u]].from;        }    }    return min_cost;}int mp[maxn][maxn];int main(){    int i,j;    while(~scanf("%d",&n))    {        init(); S=0; T=2*n+1;        for(i=1;i<=n;i++){            add(S,i,1,0);            add(n+i,T,1,0);        }        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                scanf("%d",&mp[i][j]);                add(i,n+j,1,mp[i][j]);            }        }        printf("%d\n",MCMF(S,T,T+1));        init(); S=0; T=2*n+1;        for(i=1;i<=n;i++){            add(S,i,1,0);            add(n+i,T,1,0);        }        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                add(i,n+j,1,-mp[i][j]);            }        }        printf("%d\n",-MCMF(S,T,T+1));    }    return 0;}




0 0
原创粉丝点击