Hdu1151 air raid-minimum path coverage

Source: Internet
Author: User

For a dag, a person can take a path, or at a point (the path length is 0), ask how many people need to cover all the points.

Based on the nature of the bipartite graph:

Minimum path coverage of Dag. After each vertex is split, find the maximum number of matching m and the result is n-M. When finding a specific path, follow the matching edge, match edge I → j ', J → K', K → l '.... form a directed path.


#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>const int maxn=125;using namespace std;int mx[maxn],my[maxn],vis[maxn],e[maxn][maxn],n,m;int path(int u){    int i;    for(i=1;i<=n;i++)    {        if(e[u][i]&&!vis[i])        {            vis[i]=1;            if(my[i]==-1||path(my[i]))            {                mx[u]=i;                my[i]=u;                return 1;            }        }    }    return 0;}int hungry(){    int res=0;    memset(mx,-1,sizeof mx);    memset(my,-1,sizeof my);    for(int i=1;i<=n;i++)    {        if(mx[i]==-1)        {            memset(vis,0,sizeof vis);            res+=path(i);        }    }    return res;}int main(){    int ans,T,a,b,i;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        memset(e,0,sizeof e);        while(m--)        {            scanf("%d%d",&a,&b);            e[a][b]=1;        }        ans=hungry();        printf("%d\n",n-ans);    }    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.