hdu1151 Air Raid --- 最小路径覆盖

给一个DAG图,一个人可以走一条路,或者就在一个点(路径长度为0),问至少需要多少人可以覆盖所有点。

根据二分图的性质:

DAG的最小路径覆盖,将每个点拆点后求最大匹配数m,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j‘,j→k‘,k→l‘....构成一条有向路径。

#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;
}

hdu1151 Air Raid --- 最小路径覆盖

时间: 2024-10-09 01:13:06

hdu1151 Air Raid --- 最小路径覆盖的相关文章

【网络流24题----02】Air Raid最小路径覆盖

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4591    Accepted Submission(s): 3072 Problem Description Consider a town where all the streets are one-way and each street leads from one

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 102   Problem Description Consider a town where all the streets are one-way and each street leads from one

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把所有的顶点都覆盖)

题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 102   Problem Description Consider a town where all the streets are one-way and each street leads from one

hdu 1151 Air Raid(最小路径覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3894    Accepted Submission(s): 2573 Problem Description Consider a town where all the streets are one-way and each street leads from on

hdu 1151 Air Raid 最小路径覆盖

Air Raid Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1151 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting

[POJ] 1422 Air Raid(最小路径覆盖)

题目地址:http://poj.org/problem?id=1422 一个地图上有n个小镇,以及连接着其中两个小镇的有向边,而且这些边无法形成回路.现在选择一些小镇空降士兵(1个小镇最多1个士兵),士兵能沿着边走到尽头,问最少空降几个士兵,能遍历完所有的小镇.最小路径覆盖问题.先拆点,将每个点分为两个点,左边是1到n个点,右边是1到n个点然后每一条有向边对应左边的点指向右边的点 因为最小路径覆盖 = 节点数 - 最大匹配数,所以匈牙利算法求一下二分图最大匹配就能得出结果了. 1 #includ

Air Raid(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

hdu1151+poj2594(最小路径覆盖)

传送门:hdu1151 Air Raid 题意:在一个城镇,有m个路口,和n条路,这些路都是单向的,而且路不会形成环,现在要弄一些伞兵去巡查这个城镇,伞兵只能沿着路的方向走,问最少需要多少伞兵才能把所有的路口搜一遍. 分析:有向无环图不相交最小路径覆盖数,等于节点数减去二分图的最大匹配数,对于每条弧,弧头作为X部,弧尾作为Y部.最后在求得最大匹配的基础上,没有被匹配的Y部的点就是简单路径的起点.其个数刚好就是节点数减去二分图的最大匹配数. #include <cstdio> #include

POJ1422-Air Raid(最小路径覆盖)

题目链接 题意:给定一个有向图,问最少放多少个伞兵,使得所有路口都能被走到,所有路口有且只被走到一次. 思路:二分图的最小路径覆盖. 在一个 N*N 的有向图中,路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联;(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次);如果不考虑图中存在回路,那么每每条路径就是一个弱连通子集. 由上面可以得出: 1.一个单独的顶点是一条路径; 2.如果存在一路径 p1,p2,