# 二分圖_HDOJ2853

Assignment
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1748 Accepted Submission(s): 940

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

AC代碼：

``````#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 55;
const int inf = 0x3f3f3f3f;

int n, m, ans, ans1, ans2;
int w[maxn][maxn], la[maxn], lb[maxn], match[maxn];
bool vis[maxn][maxn], va[maxn], vb[maxn];
int delta;

inline bool find(int x) {
va[x] = true;

for(int y = 1; y <= m; y ++) {
if(la[x] + lb[y] == w[x][y]) {
if(!vb[y]) {
vb[y] = true;
if(!match[y] || find(match[y])) {
match[y] = x;
return true;
}
}
}
}

return false;
}

inline void KM(void) {
for(int i = 1; i <= n; i ++) {
la[i] = inf;

for(int j = 1; j <= m; j ++) {
lb[j] = 0;
la[i] = max(la[i], w[i][j]);
}
}

memset(match, 0, sizeof match);
ans2 = 0;

for(int i = 1; i <= n; i ++)
while(true) {
memset(va, false, sizeof va);
memset(vb, false, sizeof vb);

if(find(i)) break;

delta = inf;
for(int x = 1; x <= n; x ++)
if(va[x])
for(int y = 1; y <= m; y ++)
if(!vb[y])
delta = min(delta, la[x] + lb[y] - w[x][y]);

for(int j = 1; j <= n; j ++)
if(va[j]) la[j] -= delta;

for(int j = 1; j <= m; j ++)
if(vb[j]) lb[j] += delta;
}

for(int i = 1; i <= m; i ++) {
int tmp = match[i];
ans2 += w[tmp][i];
}
}

int main(void) {
//freopen("in.txt", "r", stdin);

while(scanf("%d%d", &n, &m) != EOF) {
ans = 0;
//輸入
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
scanf("%d", &w[i][j]);

for(int i = 1; i <= n; i ++) {
int j;
scanf("%d", &j);
ans += w[i][j];
vis[i][j] = true;
}

KM();
ans1 = ans2;

for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
if(vis[i][j]) w[i][j] = w[i][j] * (n + 1) + 1;
else w[i][j] = w[i][j] * (n + 1);

KM();

int permt = n - ans2 % (n + 1);
printf("%d %d\n", permt, ans1 - ans);

memset(vis, false, sizeof vis);
}

//fclose(stdin);
return 0;
}
``````