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. MySQL5.7.12新密码登录方式及密码策略

    在Centos6.6上安装MySQL5.7.12时,遇到了一个问题 安装后在/root目录下没有发现有.mysql_secret这个文件,所以没有没法按照官方文档上说的那样使用,这里记录下, 解决方式 ...

  2. react 编写组件 五

    看以下示例了解如何定义一个组件 // 定义一个组件LikeButton var LikeButton = React.createClass({ // 给state定义初始值 getInitialSt ...

  3. hbuilder用自己的服务

    2016-03-10 以后写测试demo用Sublime3 http://docs.emmet.io/cheat-sheet/ 更多炫酷信息和emmet语法请参见: 视频demo 语法文档 2016- ...

  4. JSON 与List转换类封装

    json与list转换小结: import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; impor ...

  5. Android画廊控件之Gallery

    Gallery:用来显示图片列表.可以左右拖动. 如图: 图片取自http://www.cnblogs.com/menlsh/archive/2013/02/26/2934434.html 在Gall ...

  6. 状态栏通知Notification的简单使用

    今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下. 在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示.而在Android中有提醒功能 ...

  7. Oracle数据库导入imp命令导入时1659错误处理

    今天在自己的电脑上在给数据库导入表结构及数据时报1659错误,错误如下: IMP-00017:由于oracle错误1659,以下语句失败: “create table “T_TELETE” ..... ...

  8. jenkins(二)项目构建

    通过上一篇“jenkins(一)集成环境搭建示例”,已经完成了jenkins的安装,基本配置,启动,下面继续小结jenkins使用 一.jenkins系统配置 访问jenkins,点击系统管理-> ...

  9. javascript 学习笔记之模块化编程

    题外: 进行web开发3年多了,javascript(后称js)用的也比较多,但是大部分都局限于函数的层次,有些公共的js函数可重用性不好,造成了程序的大量冗余,可读性差(虽然一直保留着注释的习惯,但 ...

  10. 文字以及div水平垂直居中

    文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div>< ...