HDU 1853 Cyclic Tour

Source: Internet
Author: User

HDU_1853

First, if you want to ensure that a graph has a ring and there is no intersection between the rings, the outbound and inbound degrees of each vertex must be 1. Therefore, we can split a vertex into two vertices, represent the degree of exit and inbound respectively, and then find the perfect match of the Bipartite Graph after the split point.

I have come up with two ideas on how to determine whether the source image can constitute a perfect match:

① Before using the KM algorithm, use the Hungary algorithm to find the maximum match. If the maximum match number is N, there will be a perfect match.

② We can start with the slack variable of the KM algorithm. Every time we update the slack, we are trying to increase the number of points that are not originally in the staggered tree, after all the values that can be added to the midpoint of the staggered tree are added, slack will not be updated. In other words, if the slack is no longer updated, all vertices that can be added to the staggered tree must be added. At this time, the augmented path cannot be found. Therefore, when slack is no longer updated, we can conclude that the source image cannot constitute a perfect match. Of course, the premise is that we need to put slack in the for (;), and initialize it to INF every time before trying to increase the number.

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