HDU 2426 Interesting Housing Problem

Source: Internet
Author: User

HDU_2426

This topic is a perfect match for the maximum right. we need to pay attention to two points: 1. Deal with the negative edge. 2. Note that N and M are not necessarily equal.

For the processing of the first point, I have seen two ways to deal with: ① all the sides are initialized to the negative side, so that as the N-M between all the edge is to exist to match, if the number of matches with the positive edge weight is N, there is a solution. Otherwise, no solution is available. ② The edge with negative values is regarded as nonexistent, and in the KM algorithm process, if the current state cannot be extended, there is no solution.

For the second point of processing, some of the AC programs I have seen did not notice this problem. If we do not deal with this point, if N> M data appears and the first point is processed in the first method, the program will be in an endless loop.

What puzzles me about this question is that slack optimization of the km algorithm that can be found on the Internet is in two forms: array and single variable, I don't think there are any essential differences between the two forms. At the same time, I think array forms should be less efficient theoretically, however, the program I write in the form of a single variable (the first point is handled in the ② method, because the first method will be slower) will run around milliseconds, the programs I found written in the form of arrays (the first method is used to process the first point, and the second method is much slower to process) can only run less than 400 ms, this is confusing to me.

#include<stdio.h>
#include<string.h>
#define MAXD 510
#define INF 1000000000
int N, M, E, G[MAXD][MAXD];
int yM[MAXD], A[MAXD], B[MAXD];
int visx[MAXD], visy[MAXD], slack;
void init()
{
int i, s, r, v;
memset(G, -1, sizeof(G));
for(i = 0; i < E; i ++)
{
scanf("%d%d%d", &s, &r, &v);
if(G[s][r] == -1 || v > G[s][r])
G[s][r] = v;
}
}
int searchpath(int u)
{
int v, temp;
visx[u] = 1;
for(v = 0; v < M; v ++)
if(G[u][v] != -1 && !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;
}
int EK()
{
int i, j, u, v;
for(i = 0; i < N; i ++)
{
A[i] = -1;
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(;;)
{
slack = INF;
memset(visx, 0, sizeof(visx));
memset(visy, 0, sizeof(visy));
if(searchpath(u))
break;
if(slack == INF)
return 0;
for(i = 0; i < N; i ++)
if(visx[i])
A[i] -= slack;
for(i = 0; i < M; i ++)
if(visy[i])
B[i] += slack;
}
}
return 1;
}
void printresult()
{
int i, res = 0;
for(i = 0; i < M; i ++)
if(yM[i] != -1)
res += G[yM[i]][i];
printf("%d\n", res);
}
int main()
{
int t = 0;
while(scanf("%d%d%d", &N, &M, &E) == 3)
{
init();
printf("Case %d: ", ++t);
if(EK())
printresult();
else
printf("-1\n");
}
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.