Assignment

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
 
 
【题意】
  有N个公司,M个任务,(N<=M)每个公司做每个任务都有一个效率值,每个公司开始都安排了一个任务,求调整之后效率值总和最大,并使调整的公司尽量少
 
【分析】
  

  哇塞这题建图好巧妙!!!先要效率和最大,然后调整次数最少,把每条边的权值扩大k倍(k>n),然后属于原始任务的边权值+1,权值加1是为了当两条边权值相同时,更优先选择属于原始任务的边,扩大k倍的巧妙之处不仅在于KM匹配时优先选择原始边所得答案除k得到原始答案,而且结果对k求余就是保留的就是原始任务的数量。
  感觉,如果两边点数不一样,然后求最佳完备匹配的话呢,要把点数小的放左边,然后直接求,根据KM算法的步骤,是会先算到最大的那一个的,因为顶标是不断减小的??

  (其实我也不是很清楚)

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 110
#define Maxm 3010
#define INF 0xfffffff struct node
{
int x,y,c,next;
}t[Maxm];int len;
int first[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} int w[Maxn][Maxn]; int lx[Maxn],ly[Maxn];
int slack[Maxn],match[Maxn];
bool visx[Maxn],visy[Maxn]; int n,m; bool ffind(int x)
{
visx[x]=;
for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
{
int y=t[i].y;
if(t[i].c==lx[x]+ly[y])
{
visy[y]=;
if(!match[y]||ffind(match[y]))
{
match[y]=x;
return ;
}
}
else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
}
return ;
} void solve()
{
memset(ly,,sizeof(ly));
memset(match,,sizeof(match));
for(int i=;i<=n;i++)
{
lx[i]=-INF;
for(int j=first[i];j;j=t[j].next)
lx[i]=mymax(lx[i],t[j].c);
}
int i;
for(i=;i<=n;i++)
{
for(int j=;j<=m;j++) slack[j]=INF;
while()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(ffind(i)) break;
int delta=INF;
for(int j=;j<=m;j++) if(!visy[j])
delta=mymin(delta,slack[j]);
if(delta==INF) return;
for(int j=;j<=n;j++) if(visx[j]) lx[j]-=delta;
for(int j=;j<=m;j++)
if(visy[j]) ly[j]+=delta;
else if(slack[j]!=INF) slack[j]-=delta;
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
len=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&w[i][j]);
w[i][j]*=(n+);
}
int sum=,ans=;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
sum+=w[i][x];
w[i][x]++;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
ins(i,j,w[i][j]);
} solve();
for(int i=;i<=m;i++) if(match[i]) ans+=lx[match[i]]+ly[i];
printf("%d %d\n",n-ans%(n+),(ans-sum)/(n+));
}
return ;
}

[HDU 2853]

2016-10-27 13:17:06

【HDU 2853】Assignment (KM)的更多相关文章

  1. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

  2. 【UVA 1411】 Ants (KM)

    Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...

  3. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  4. 【HDU 6005】Pandaland(Dijkstra)

    Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...

  5. 【HDU - 3533】Escape(bfs)

    Escape  Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...

  6. 【HDU - 6581】Vacation(思维)

    Vacation 题意 有n+1辆车,属性有长度l,距离终点的距离s,速度v问你最末尾的车到达终点的时间 Sample Input 1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1 ...

  7. 【HDU 5750】Dertouzos(数学)

    题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...

  8. 【HDU 2955】Robberies(DP)

    题意是给你抢劫每个银行可获得的钱m和被抓的概率p,求被抓的概率小于P,最多能抢多少钱.01背包问题,体积是m,价值是p.被抓的概率不是简单相加,而应该是1−Π(1−p[i])DP:dp[i]表示抢到i ...

  9. 【HDU 6000】Wash(贪心)

    Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...

随机推荐

  1. 在虚拟机安装64位系统提示,此主机支持Intel VT-x,但Intel VT-x处于禁用状态

    进入BIOS - Security - Virtualization - Intel (R) Virtualization Technology 将 Disabled 改为 Enabled 即可

  2. I2C Verilog的实现(二)

    1. 起始结束信号的判断 //--------------------------------------------- //start,stop condition judgement //---- ...

  3. java内存分块

     运行时数据区域 Java虚拟机在执行Java的过程中会把管理的内存划分为若干个不同的数据区域.这些区域有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,而有的区域则依赖线程的启 ...

  4. asterisk webrtc使用SIPML5初体验

    一直尝试,web呼叫xlite终端没有,主要是配置问题: 其中sip.conf配置如下: [general] context=public ; Default context for incoming ...

  5. 结合Retrofit,RxJava,Okhttp,FastJson的网络框架RRO

    Retrofit以其灵活的调用形式, 强大的扩展性著称. 随着RxAndroid的推出, Retrofit这样的可插拔式的网络框架因其可以灵活兼容各种数据解析器, 回调形式(主要还是RxJava啦)而 ...

  6. OC中的字符串常用方法

    OC中的字符串常用方法 OC中对字符串进行操作使用了Foundation框架中的NSString类(不可变).NSMutableString类(可变). NSString 1.创建字符串 [objc] ...

  7. com和dll(一)

    1.com为一种规范,使不同语言能在二进制层面上进行交互. 2.dll为ms的一种程序打包文件,使程序的代码能分开打包编译,并能在运行时共享.

  8. SQL Server系统视图 [不定期更新]

    1.sys.objects:在数据库中创建的每个用户定义的架构作用域内的对象(如表.视图.约束.默认值.日志.规则存储过程等,但不包括DDL触发器)在该表中均对应一行. 列名 说明 name 对象名. ...

  9. 开发错误日志之Unix/Linux命令未执行或无结果等且程序无错误

    在Unix/Linux环境中开发时,特别要注意权限问题,否则经常找不到错误的原因,其实就是因为权限所致.

  10. C#基础(一)——C#中反斜杠/n与/r的区别

    最近在公司实习的过程中,遇到了字符串换行的问题,百度了一下,发现字符串换行的问题还挺多,总结一下最基本的点,以防忘记. \n—>换行符(New Line),作用为换行符后面的字符串显示到“下一行 ...