J - Air Raid - hdu 1151(最小边覆盖)

题意:给一个有向无环图,求出来最少需要几个士兵可以遍历所有的边。

分析:有向无环图的最小边覆盖 = 点数 - 最大匹配数

为什么是这样的公式??可以思考一下,如果这N个点之间没有边,是不是应该有N个士兵去查看,但是如果增加一条边就应该减去这条边,以此类推,公式就比较容易明白了。

********************************************************************

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

const int MAXN = 205;
const int oo = 1e9+7;

bool G[MAXN][MAXN], used[MAXN];
int My[MAXN], N;

bool Find(int i)
{
    for(int j=1; j<=N; j++)
    {
        if( G[i][j] && used[j] == false )
        {
            used[j] = true;
            if( !My[j] || Find(My[j]))
            {
                My[j] = i;
                return true;
            }
        }
    }

return false;
}

int main()
{
    int T;

scanf("%d", &T);

while(T--)
    {
        int i, M, u, v, ans=0;

scanf("%d%d", &N, &M);

memset(G, false, sizeof(G));
        memset(My, false, sizeof(My));

for(i=1; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            G[u][v] = true;
        }

for(i=1; i<=N; i++)
        {
            memset(used, false, sizeof(used));
            if( Find(i) == true )
                ans++;
        }

printf("%d\n", N-ans);
    }

return 0;
}

时间: 2024-12-15 07:14:04

J - Air Raid - hdu 1151(最小边覆盖)的相关文章

J - Air Raid - hdu 1151 (最小路径覆盖+闭包传递)

题意:给一个有向无环图,求出来最小路径覆盖,注意一个点可能会被多条路径重复 分析:因为有可能多条路径走一个点,可又能会造成匹配的不完全,所以先进行一次闭包传递(floyd),然后再用二分匹配的方法求出来最大匹配即可. *********************************************************************. #include<stdio.h>#include<string.h>#include<queue>using n

POJ1422 Air Raid 【DAG最小路径覆盖】

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6763   Accepted: 4034 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

POJ 1422 &amp;&amp; ZOJ 1525 --Air Raid【二分图 &amp;&amp; 最小路径覆盖】

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 4434 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

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

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 intersection and walking through town's streets you can never reach the same intersection i.e. the town's

POJ1422 Air Raid【二分图最小路径覆盖】

题目链接: http://poj.org/problem?id=1422 题目大意: 有N个地点和M条有向街道,现在要在点上放一些伞兵,伞兵可以沿着有向街道走,直到不能走为止. 每条边只能被一个伞兵走一次.问:至少放多少伞兵,能使伞兵可以走到图上所有的点. 思路: 很明显的最小路径覆盖问题.先转换为二分图,先将N个点每个点拆成两个点,左边是1~N个点,右 边也是1~N个点.将有向街道变为左边点指向右边点的边. 因为二分图最小路径覆盖 = 点数 - 二分图最大匹配数,则求出结果就是放的最少伞兵数.

HDU1151 Air Raid(有向图最小路径覆盖)

题意: N个点M条边的有向图 意思就是问最小覆盖 思路: 有向图建单向边,然后匈牙利求最大匹配数 用N-最大匹配就可以了 /* *********************************************** Author :devil Created Time :2016/5/17 11:55:14 ************************************************ */ #include <cstdio> #include <cstring

hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 题目大意: 城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少派多少人去 思路: 转化题意,求用最少的有向边覆盖点        -------->      最小边覆盖数目=顶点数-最大二分匹配数目 注意:这道题目中说得是有向无环的DAG,所以顶点数目不能按照2倍来计算 在DAG中我们假设点集为 -----> Ni     然后建立每个点对应的虚点 ---

HDU 1151 Air Raid(最小路径覆盖 = 顶点数 - 最大匹配数)

Air Raid Problem 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 intersection and walking through town's streets you can never reach the same

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

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