cogs 728. [24 network flow problem] Hungarian algorithm minimum path coverage problems

728. [24 network flow problem] minimum path coverage problems

★★★ ☆ input file: path3.in   output file: path3.out   Evaluation plugin
Time limit: 1 s Memory limit: 128 MB

Algorithm shortest path problem 8-3 covering problem (Exercises 8-13)

 

Problem Description:

Given a directed graph G = (V, E). Let P be a set of simple paths G (vertex disjoint) a. If V each

 

Vertex P is just one way, is called a path P G coverage. Any path from the top P of the V

 

Point, length is arbitrary, in particular, may be zero. G is the minimum path coverage path contained the least number of G

 

The path coverage.

 

Design of an efficient algorithm for the minimum coverage to a path acyclic graph G.

 

prompt:

Set V = {1,2, ..., n}, the network configuration G1 = (V1, E1) as follows:

1 are the capacity of each edge. G1 is seeking network (x0, y0) maximum flow.

 

'Programming tasks:

For a given for a given directed acyclic graph G, G programmed to identify a minimum path coverage.

 

data input:

Provide input data from the file input.txt. File 1 line has two positive integers n and m. n is the given directed acyclic graph

 

Number of vertices of G, m is the number of edges of G. The next m rows, each row having two positive integers i and j, represents a directed edge (i, j).

 

'Output Results:

When the run is finished, the minimum output to a file path coverage in output.txt. Start from the first row, each output line

 

Path. The last line of the file is the minimum number of paths.

 

Input File Example

input.txt 

 

11 12
1 2 1 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 11 10 11

 

 Output File Example

output.txt

 

1 4 7 10 11 2 5 8 3 6 9 3

 

 

 

 

 

 

 

explain:

Coding!

 

He found himself too weak

This seems a relatively simple bipartite graph shortest path covering problems can go wrong Oh

 

Error-prone points and now we are speaking about the questions of

1. The number is the minimum path coverage nodes n - the maximum number of matches bipartite graph

2. The questions allow the output path then it should be how to do it?

I thought of a way to look like this

 

First, we enumerate the node running the algorithm dfs Hungary at the time that the accumulated time ans small to large enumeration not do

That is to say that hav arrays under normal circumstances is [the] hav = hav [before] but if this is not the way we want to compare the output path because I scratch (small) starts output

 

Then we can look at a son array and the array is just the other way around hav

We then set up a degree in the array to store

So after the above treatment

 

We just need to enumerate the n points if this point is 0 degrees then we start down the son ran from this point onwards it will be able to print out the trail

 

 

Look at the code to understand the specific content slightly (in fact it is quite simple long-winded so much I still look at the code bit more clear it)

 

Masterwork:

 

#include<bits/stdc++.h>
#define maxn 205
using namespace std;
int n,m;
int vis[maxn],hav[maxn],tim=0;
vector<int> v[maxn];
int in[maxn];
int son[maxn];
bool Dfs(int x)
{
    for(int i=0;i<v[x].size();i++)
    {
        int y=v[x][i];
        if(vis[y]!=tim)
        {
            vis[y]=tim;
            if(!hav[y]||Dfs(hav[y]))
            {
                hav[y]=x;
                son[x]=y;
                in[y]++;
                return true;
            }
        }
    }
    return false;
}
bool Printed[maxn];
void Print(int i)
{
    int x=i;
    while(x!=0)
    {
        printf("%d ",x);
        Printed[x]=true;
        x=son[x];
    }
    putchar('\n');
}
int main()
{
    freopen("path3.in","r",stdin);
    freopen("path3.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        tim++;
        ans+=Dfs(i);
    }
    for(int i=1;i<=n;i++)
        if(in[i]==0)
            Print(i);
    printf("%d\n",n-years);
    return  0 ; 
}

 

Point a walk Zanga followers chant

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11408305.html