Assignment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1068    Accepted Submission(s): 551

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

题意:n个公司m个任务。每一个公司仅仅接受一个任务。每一个任务仅仅被一个公司接受,每一个公司i接受任务j的做事效率为g[i][j],如今已经分配好了。问如何改变任务分配能够让效率最大,求出要修改的公司数目和添加的效率。

思路:与hdu3315相似,hdu 3315;若点数为N,则把每条边的权值扩大x倍(x>N),若是原有匹配。则再把权值加1。最后KM算法求出ans,则最大权值之和=ans/x。没有被修改的=ans%x。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; /*
KM算法 O(nx*nx*ny)
求最大权匹配(最佳匹配)
若求最小权匹配,可将权值取相反数,结果取相反数
点的标号从0開始
*/ const int N=110;
int nx,ny; //两边的点数
int g[N][N]; //二分图描写叙述,g赋初值为-INF
int linker[N],lx[N],ly[N]; //y 中各点匹配状态。x,y中的点的标号
int slack[N];
bool visx[N],visy[N];
bool flag; bool DFS(int x)
{
visx[x]=true;
for (int y=0;y<ny;y++)
{
if (visy[y]) continue;
int tmp=lx[x]+ly[y]-g[x][y];
if (tmp==0)
{
visy[y]=true;
if (linker[y]==-1||DFS(linker[y]))
{
linker[y]=x;
return true;
}
}
else if (slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
flag=true;
memset(linker,-1,sizeof(linker));
memset(ly,0,sizeof(ly));
for (int i=0;i<nx;i++) //赋初值。lx置为最大值
{
lx[i]=-INF;
for (int j=0;j<ny;j++)
{
if (g[i][j]>lx[i])
lx[i]=g[i][j];
}
}
for (int x=0;x<nx;x++)
{
for (int i=0;i<ny;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=0;i<ny;i++)
if (!visy[i]&&d>slack[i])
d=slack[i];
for (int i=0;i<nx;i++)
if (visx[i])
lx[i]-=d;
for (int i=0;i<ny;i++)
{
if (visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
}
int res=0;
for (int i=0;i<ny;i++)
{
if (linker[i]==-1||g[linker[i]][i]<=-INF) //有的点不能匹配的话return-1
{
flag=false;
continue;
}
res+=g[linker[i]][i];
}
return res;
}
//记得nx和ny初始化! !!! !! 。! int n,m; int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
int i,j,x;
while (~sff(n,m))
{
nx=n;
ny=m;
int sum=0;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
{
sf(x);
g[i][j]=x*100;
}
for (i=0;i<n;i++)
{
sf(x);x--;
sum+=g[i][x];
g[i][x]++;
}
int ans=KM();
printf("%d %d\n",n-ans%100,ans/100-sum/100);
}
return 0;
}
/*
3 3
2 1 3
3 2 4
1 26 2
2 1 3
2 3
1 2 3
1 2 3
1 2
*/

Assignment (HDU 2853 最大权匹配KM)的更多相关文章

  1. Assignment HDU - 2853(二分图匹配 KM 新边旧边)

    传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...

  2. hdu 2255 奔小康赚大钱 最大权匹配KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事 ...

  3. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  4. 二分图 最大权匹配 km算法

    这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...

  5. Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好 ...

  6. Uvalive 4043 Ants —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...

  7. HDU3488 Tour —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit ...

  8. 二分图最大权匹配——KM算法

    前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...

  9. hdu 2426 Interesting Housing Problem 最大权匹配KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...

随机推荐

  1. Laravel 中自定义日志目录

    参考:https://laravel-china.org/articles/7125/custom-log-directory-in-laravel

  2. Linux 硬盘挂载方法

    linux 硬盘分区,分区,删除分区,格式化,挂载,卸载笔记 硬盘挂载操作工作步骤: 1.先查看目前机器上有几块硬盘,查看命令有两种: 命令1:# fdisk –l 命令2:# dmesg | gre ...

  3. apache2.2控制目录文件访问设置

    1.apache2.2控制目录文件访问需求 红框可以访问,其他不能访问 2.apache2.2具体正则配置 <locationMatch ^/f/user/Panorama/81/581/(gr ...

  4. Oracle 入门学习笔记

    linux命令 查看linux系统版本号 uname -r 或 uname -a 查看linux发行版本号 cat /etc/redhat-release 查看linux具体版本号 cat /proc ...

  5. import xxx from 和 import {xxx} from的区别

    1.vue import FunName from ‘../xxx’ 1.js export defualt function FunName() { return fetch({ url: '/ar ...

  6. ASN.1笔记——语法规则与类型概述

    转载:https://blog.csdn.net/sever2012/article/details/7672699 一.简介 ASN.1(Abstract Syntax Notation doton ...

  7. mysql中行转列与列传行的问题

    行转列: 使用cross join 的方式 使用case-when的方式 列转行: SELECT user_name, REPLACE ( substring_index(mobile, ',', a ...

  8. OOD沉思录 --- 继承

    一,继承只应被用来为特化层次结构建模 实际上也就是要满足LSP原则,水果类<-榴莲的继承是特化   二,派生类必须知道他们的基类,基类不应当知道他们的派生类   复用的前提   三,基类中的所有 ...

  9. Windows 如何远程登陆 Server 的 jupyter

    jupyter 安装就不用赘述了,本示例以 Putty 为例,展示如何从本地 windows 系统调用远程的 jupyter notebook 并且在本地 Chrome 中打开的方法 1. 首先,ss ...

  10. centos7.3挂在移动硬盘(亲测)

    一 下载ntfs-3g wget https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2016.2.22.tgz 二 解压并安装 1 检测是否安装gcc r ...