Bipartite graph and network flow

One, bipartite graph

1. Bipartite graph matching (Hungarian algorithm)

The essence of the Hungarian algorithm is to search for augmented paths in the bipartite graph, because each augmented path can be negated to make the maximum match +1.

It can be proved that if at a certain moment, no augmentation path starting from x is found, it will not be found after a few rounds of augmentation.
So you only need to traverse the points on the left once, and find an augmented path for each point.

What needs attention is the function of vis[i], because the augmented path is u −> to −> u ′ −> to ′... U->to->u'->to'...u ->to>u>to' ...Because we know there is no augmenting path ring, it is necessary to markvis [to] vis [to]v i s [ t o ] to avoid the generation of loops.

bool dfs(int x)
{
    
    
    for(auto to:v[x])if(vis[to]==0)
    {
    
    
        vis[to]=1;
        if(belong[to]==0)
        {
    
    
            belong[to]=x;
            return true;
        }
        else if(dfs(belong[to]))
        {
    
    
            belong[to]=x;
            return true;
        }
    }
    return false;
}
rep(i,1,n)
{
    
    
    memset(vis,0,sizeof(vis));
    if(dfs(i))tot++;
}

Example 1.1.1 P3386 [Template] Bipartite graph maximum matching
example 1.1.2 P2055 [ZJOI2009] Holiday dormitory
example 1.1.3 P2756 Pilot matching scheme problem

2. Maximum matching of bipartite graphs (KM algorithm)

The maximum matching of bipartite graphs is to find the maximum value of the sum of edge weights on the basis of perfect matching .
Greedy, if each left point can match his largest edge, the match obtained at this time is the largest match.

So we give each left point a superscript (the initial value is the maximum value of all its connected edge weights) to indicate the edge weights that this point can match. It is easy to know that if the match is successful at this time, it is the maximum match, but this may not be a complete match.

So we need to modify the superscript of the left point so that the superscript and the reduction are the smallest, and the match can be found. It is easy to know that this time is the maximum match. The essence of the KM algorithm is to achieve this modification.

It is easy to know that, like the Hungarian algorithm, the order of point matching does not affect the final answer, so we only need to add points in order. For each point, modify the superscript to match the point. Such greedy modification can be obtained. The global optimal solution.

KM algorithm (understanding articles)
KM algorithm (application articles)
[Algorithm notes] Bipartite graph maximum weight matching-KM algorithm (dfs version O(n4) + bfs version O(n3))

Example 1.2.1 P6577 [Template] Bipartite graph maximum weight perfect match A
very card example, you have to use bfs to pass, and I can't pass it with the adjacency table to save the graph.

Remaining question 1: Why does the km algorithm of dfs in the above example question TLE
legacy question 2: Why does the km algorithm of bfs+ adjacency table in the above example question TLE
legacy question 3: why the slack array slake value of non-interlaced edges should be subtracted by delta

例 1.2.2 HDU-2853 Assignment

In general, it is km algorithm matching for optimal matching. But a little bit of operation has been added, so that when matching, the assigned tasks are matched first.
We expand the original weight by k times to make it a multiple of k. And those tasks that have been assigned, we +1. This operation is convenient for counting while allowing km to match this task first (because the km algorithm is similar to a greedy algorithm, the priority is matched with a large weight, and +1, because the number larger than it is at least k times larger, it will not change The matching result, which is equal to it, is because it is +1, so it is matched first) Then the number that is unchanged for the statistics is: ans%k, how many 1s are counted. The number of changes is: n-ans%k. For the total weight of matching: Divide by k and round up.

Guess you like

Origin blog.csdn.net/solemntee/article/details/114388900