【HDU 2853】Assignment (KM)
Assignment
Problem DescriptionLast 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.InputFor 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.OutputFor 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 Input3 3
2 1 3
3 2 4
1 26 2
2 1 3
2 3
1 2 3
1 2 3
1 2Sample Output2 26
1 2Source
哇塞这题建图好巧妙!!!先要效率和最大,然后调整次数最少,把每条边的权值扩大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)的更多相关文章
- 【HDU - 3085】Nightmare Ⅱ(bfs)
-->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...
- 【UVA 1411】 Ants (KM)
Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...
- 【HDU - 4345 】Permutation(DP)
BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...
- 【HDU 6005】Pandaland(Dijkstra)
Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...
- 【HDU - 3533】Escape(bfs)
Escape Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...
- 【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 ...
- 【HDU 5750】Dertouzos(数学)
题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...
- 【HDU 2955】Robberies(DP)
题意是给你抢劫每个银行可获得的钱m和被抓的概率p,求被抓的概率小于P,最多能抢多少钱.01背包问题,体积是m,价值是p.被抓的概率不是简单相加,而应该是1−Π(1−p[i])DP:dp[i]表示抢到i ...
- 【HDU 6000】Wash(贪心)
Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...
随机推荐
- css3绘制中国结
<!doctype html> <html> <head> <title></title> <meta charset='utf-8' ...
- 如何用eclipse搭建Android的开发环境
l开发主要应用Eclipse 3.7版本. l辅助工具为jdk.Androidsdk Android环境搭建 –1.1.JDK安装 –1.2.Eclipse安装 –1.3.Android SDK安 ...
- 20160402javaweb 开发模式
开发案例: 首先,我们确定用xml文件代替数据库,便于测试 建立web工程,基本架构见下图 代码如下: 首先是javabean:User.java package com.dzq.domian; im ...
- 通过Web.config中的configSections配置自己系统的全局常量
通过Web.config中的configSections配置自己系统的全局常量 随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧 首先在Web.C ...
- Ubuntu 使用top/free查看内存占用大的原因
Ubuntu 使用top/free查看内存占用大的原因 linux/ubuntu下free/top查看内存占用大的原因 使用free/top查看内存占用的时候,吓了一大跳,机器4GB的内存,显 ...
- OC - 5.内存管理
一.引用计数器 1> 栈和堆 栈 ① 主要存储局部变量 ② 内存自动回收 堆 ① 主要存储需要动态分配内存的变量 ② 需要手动回收内存,是OC内存管理的对象 2> 简介 作用 ① 表示对象 ...
- js实现一个砖头在页面拖拉效果
用js实现一个砖头在页面,但鼠标点击拖动时,砖头在页面上形成拖拉效果: 刚开始时: 鼠标点击拖动后: 实现代码: <html> <head> <meta ...
- java ,js获取web工程路径
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...
- ubuntu 13.10自定义启动顺序
添加PPA sudo add-apt-repository ppa:danielrichter2007/grub-customizer sudo apt-get update sudo apt-get ...
- Android中View绘制流程以及invalidate()等相关方法分析(转载的文章,出处在正文已表明)
转载请注明出处:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时 ...