HDU 2583 Assignment

Source: Internet
Author: User

HDU_2853

We can think that if we start matching from scratch and if the final part of matching changes but does not affect the final result, we do not need to change it into a new matching method. Therefore, we consider adding a "preference" to the original edge to ensure that it matches the original edge first without affecting the result.

When processing data, we can expand each edge to a certain value (multiply by 10 in my program), and then add the original edge weight to 1, in this way, when we use KM for re-matching, it shows that the existing edge is preferentially selected for matching, and after this processing, the generation of the optimal matching will not be affected.

At the end of the statistical result, we can restore the edge weight. The number of changes is the number of edges in the optimal matching result of the Edge Weight Model 10 that is not 0.

#include<stdio.h>
#include<string.h>
#define MAXD 60
#define INF 1000000000
int G[MAXD][MAXD], N, M, sum;
int yM[MAXD], A[MAXD], B[MAXD], slack;
int visx[MAXD], visy[MAXD];
void init ()
{
int i, j;
for(i = 0; i < N; i ++)
for(j = 0; j < M; j ++)
{
scanf("%d", &G[i][j]);
G[i][j] *= 10;
}
sum = 0;
for(i = 0; i < N; i ++)
{
scanf("%d", &j);
j --;
sum += G[i][j] / 10;
G[i][j] ++;
}
}
int searchpath(int u)
{
int v, temp;
visx[u] = 1;
for(v = 0; v < M; v ++)
if(!visy[v])
{
temp = A[u] + B[v] - G[u][v];
if(temp == 0)
{
visy[v] = 1;
if(yM[v] == -1 || searchpath(yM[v]))
{
yM[v] = u;
return 1;
}
}
else if(temp < slack)
slack = temp;
}
return 0;
}
void KM()
{
int i, j, u;
for(i = 0; i < N; i ++)
{
A[i] = 0;
for(j = 0; j < M; j ++)
if(G[i][j] > A[i])
A[i] = G[i][j];
}
memset(B, 0, sizeof(B));
memset(yM, -1, sizeof(yM));
for(u = 0; u < N; u ++)
for(;;)
{
memset(visx, 0, sizeof(visx));
memset(visy, 0, sizeof(visy));
slack = INF;
if(searchpath(u))
break;
for(i = 0; i < N; i ++)
if(visx[i])
A[i] -= slack;
for(i = 0; i < M; i ++)
if(visy[i])
B[i] += slack;
}
}
void printresult()
{
int i, ans = 0, cnt = 0;
for(i = 0; i < M; i ++)
if(yM[i] != -1)
{
if(G[yM[i]][i] % 10 == 0)
cnt ++;
ans += G[yM[i]][i] / 10;
}
printf("%d %d\n", cnt, ans - sum);
}
int main()
{
while(scanf("%d%d", &N, &M) == 2)
{
init();
KM();
printresult();
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.