HDU 1150 Bipartite Graph Matching minimum vertex coverage

Source: Internet
Author: User

There is a sequence of work to complete. Now there are two machines A, B, and machine A in n modes, and machine B in M modes, each task can be completed in the mode on machine A, or in the B mode of machine B. You need to restart the machine when changing the mode and ask you the minimum number of times to restart the machine.

Train of Thought: This question can regard the mode of two machines as a bipartite graph, and connect the mode of one working machine to another, then obtain the minimum vertex overwrite of the Bipartite Graph (use the least vertex to connect all edges to at least a vertex (the least vertex overwrite) = The maximum matching of the Bipartite Graph ), note that the mode 0 must be ignored (because the initial mode of the machine is 0)

Summary: This question is hard to think about, but there is still one thing that can provide us with clues: two machines, two machines --- the bipartite graph? This idea is quite good, and the awareness of the Bipartite Graph has to be strengthened.

Code:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 200;struct edge{    int u,v,next;}e[maxn * maxn];int head[maxn],match[maxn],n,m,K,tot;bool vis[maxn];void init();void solve();void add_edge(int u,int v);bool dfs(int u);int hungrian();int main(){    while(scanf("%d",&n) != EOF && n){        scanf("%d%d",&m,&K);        init();        for(int i = 1;i <= K;i ++){            int u,v;            scanf("%d%d%d",&u,&u,&v);            if(u && v)add_edge(u,v + n);        }        solve();    }    return 0;}void init(){    int size = n + m;    for(int i = 0;i < size;i ++)        head[i] = match[i] = -1;    tot = 0;}void add_edge(int u,int v){    e[tot].u = u , e[tot].v = v;    e[tot].next = head[u],head[u] = tot ++;}bool dfs(int u){    for(int i = head[u];i != -1;i = e[i].next){        int v = e[i].v;        if(!vis[v]){            vis[v] = true;            int tmp = match[v];            match[v] = u;            if(tmp == -1 || dfs(tmp))                return true;            match[v] = tmp;        }    }    return false;}int hungrian(){    int size = n + m,max_match = 0;    for(int i = 0;i < size;i ++){        memset(vis,false,sizeof(vis));        if(dfs(i)) max_match ++;    }    return max_match;}void solve(){    int ret = hungrian();    printf("%d\n",ret);}

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.