HDU 2853 Assignment 最大匹配+求最少改动的匹配数 之神奇建图

点击打开链接

 

Assignment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 756    Accepted Submission(s): 400


Problem Description
Last year a terrible earthquake attacked Sichuan province. About 300,000 PLA soldiers attended the rescue, also ALPCs. Our mission is to solve difficulty problems to optimization the assignment of troops. The assignment is measure by efficiency, which is an integer, and the larger the better.
We have N companies of troops and M missions, M>=N. One company can get only one mission. One mission can be assigned to only one company. If company i takes mission j, we can get efficiency Eij.
We have a assignment plan already, and now we want to change some companies’ missions to make the total efficiency larger. And also we want to change as less companies as possible.
 


 

Input
For each test case, the first line contains two numbers N and M. N lines follow. Each contains M integers, representing Eij. The next line contains N integers. The first one represents the mission number that company 1 takes, and so on.
1<=N<=M<=50, 1<Eij<=10000.
Your program should process to the end of file.
 


 

Output
For each the case print two integers X and Y. X represents the number of companies whose mission had been changed. Y represents the maximum total efficiency can be increased after changing.
 


 

Sample Input
  
  
3 3 2 1 3 3 2 4 1 26 2 2 1 3 2 3 1 2 3 1 2 3 1 2
 


 

Sample Output
  
  
2 26 1 2
 


 

Source
 


 

Recommend
gaojie
 

 

题意是说有n个部队和m个任务,每个任务只能由一个部队完成,并且不同的部队完成不同的任务都有一个效率值。并且一开始已经给你哪个部队做哪个任务,让你求最少改动几个部队使得获得的效率值之差(最高的效率值-一开始给你的部队匹配任务的效率值)最高。

一开始直接求出最高效率值,然后减去已经给你的部队所匹配任务的效率值,第二问就求出来了。我用match数组将求得最大的效率值后的匹配和一开始它给你的进行比较,就得出改动的部队了。但是答案错了。题目要求的是最小的部队改动数,于是参考网上的题解:

因为我们要变动最小,所以对在原计划中的边要有一些特殊照顾,使得最优匹配时,尽量优先使用原计划的边,这样变化才能是最小的且不会影响原匹配。

根据这个思想,我们可以把每条边的权值扩大k倍,k要大于n。然后对原计划的边都+1。精华全在这里。我们来详细说明一下。

全部边都扩大了k倍,而且k比n大,这样,我们求出的最优匹配就是k倍的最大权值,只要除以k就可以得到最大权值。实现原计划的边加1,这样,在每次选择边时,这些变就 有了优势,就会优先选择这些边。假如原计划的h条边被选入了最优匹配中,这样,最优权值就是k倍的最大权值+k(原计划的每条边都+1)。但是k大于n的用意何在呢?我们发现假如原计划的边全部在匹配中,只会增加n,又n<k,所以除以k后不会影响最优匹配的最大权值之和,然后我们对k取余,就正好得到加入的原计划的边的个数。这时,我们只需要用总点数-加入的原计划的点数,就可以求得最小变动数了。

 

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f
#define M 57
using namespace std;
int g[M][M],match[M],slack[M],lx[M],ly[M],s[M];
bool visx[M],visy[M];
int n,m;

bool dfs(int cur)
{
    visx[cur]=true;
    for(int y=1;y<=m;y++)
    {
        if(visy[y])continue;
        int t=lx[cur]+ly[y]-g[cur][y];
        if(t==0)
        {
            visy[y]=true;
            if(match[y]==-1||dfs(match[y]))
            {
                match[y]=cur;
                return true;
            }
        }
        else if(slack[y]>t)
        {
            slack[y]=t;
        }
    }
    return false;
}

int KM()
{
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        lx[i]=-inf;
        for(int j=1;j<=m;j++)
            if(g[i][j]>lx[i])
                lx[i]=g[i][j];
    }
    for(int x=1;x<=n;x++)
    {
        for(int i=1;i<=m;i++)
            slack[i]=inf;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))break;
            int d=inf;
        for(int i=1;i<=m;i++)
            if(!visy[i]&&d>slack[i])
                d=slack[i];
        for(int i=1;i<=n;i++)
            if(visx[i])
                lx[i]-=d;
        for(int i=1;i<=m;i++)
            if(visy[i])
                ly[i]+=d;
            else
                slack[i]-=d;
        }
    }
    int ans=0;
    for(int i=1;i<=m;i++)
    {
        if(match[i]==-1)continue;
        else
        {
            ans+=g[match[i]][i];
        }
    }
    return ans;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&g[i][j]);
                g[i][j]*=100;
            }
        int a,ans=0,count,tmp=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            ans+=g[i][a];
            g[i][a]+=1;
        }
        count=KM();
        printf("%d %d\n",n-count%100,count/100-ans/100);
    }
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值