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

题意:给一个有向无环图,求出来最小路径覆盖,注意一个点可能会被多条路径重复

分析:因为有可能多条路径走一个点,可又能会造成匹配的不完全,所以先进行一次闭包传递(floyd),然后再用二分匹配的方法求出来最大匹配即可。

*********************************************************************.

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

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

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

void Floyd()
{
    for(int k=1; k<=N; k++)
    for(int i=1; i<=N; i++)
    for(int j=1; j<=N; j++)
    {
        if( G[i][k] && G[k][j] )
            G[i][j] = true;
    }
}
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()
{
    while(scanf("%d%d", &N, &M), N+M)
    {
        int i, u, v, ans=0;

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

while(M--)
        {
            scanf("%d%d", &u, &v);
            G[u][v] = true;
        }

Floyd();

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 15:17:47

J - Air Raid - hdu 1151 (最小路径覆盖+闭包传递)的相关文章

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个点.将有向街道变为左边点指向右边点的边. 因为二分图最小路径覆盖 = 点数 - 二分图最大匹配数,则求出结果就是放的最少伞兵数.

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

题意:给一个有向无环图,求出来最少需要几个士兵可以遍历所有的边. 分析:有向无环图的最小边覆盖 = 点数 - 最大匹配数 为什么是这样的公式??可以思考一下,如果这N个点之间没有边,是不是应该有N个士兵去查看,但是如果增加一条边就应该减去这条边,以此类推,公式就比较容易明白了. ******************************************************************** #include<stdio.h>#include<string.h>

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

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

HDU 4160 最小路径覆盖

Dolls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1040    Accepted Submission(s): 496 Problem Description Do you remember the box of Matryoshka dolls last week? Adam just got another box of

HDU 1350 最小路径覆盖

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 707    Accepted Submission(s): 336 Problem Description Running a taxi station is not all that simple. Apart from the obvious dem

hdu1151 最小路径覆盖

http://acm.hdu.edu.cn/showproblem.php?pid=1151 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'