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

题意:

N个点M条边的有向图

意思就是问最小覆盖

思路:

有向图建单向边,然后匈牙利求最大匹配数

用N-最大匹配就可以了

/* ***********************************************
Author        :devil
Created Time  :2016/5/17 11:55:14
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
const int N=125;
int link[N];
bool vis[N];
vector<int>eg[N];
bool dfs(int u)
{
    for(int i=0;i<eg[u].size();i++)
    {
        int v=eg[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,m,x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(link,-1,sizeof(link));
        for(int i=1;i<=n;i++)
            eg[i].clear();
        while(m--)
        {
            scanf("%d%d",&x,&y);
            eg[x].push_back(y);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
时间: 2024-08-28 07:19:50

HDU1151 Air Raid(有向图最小路径覆盖)的相关文章

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4029    Accepted Submission(s): 2675 Problem Description Consider a town where all the st

poj 1422 Air Raid (最小路径覆盖)

链接:poj 1422 题意:有n个点和m条有向边,现在要在点上放一些伞兵,伞兵可以沿着图走, 直到不能走为止,每条边有且仅有一个伞兵走过,问最少放多少个伞兵 思路:求的最小路径覆盖,用二分图来做 对于这样的一个有向图做最小路径覆盖,首先建图 然后每一条有向边对应左边的点指向右边的点 这样建好图之后求最大匹配数 最小路径覆盖=点数-最大匹配数 [cpp] view plaincopyprint? #include<stdio.h> #include<string.h> int n,

[HDOJ1151]Air Raid(最小路径覆盖,匈牙利算法)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1151 题意:就是求最小路径覆盖,根据定义出的题. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */

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

二分图匹配(匈牙利算法的DFS实现) 初始化:g[][]两边顶点的划分情况 建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 g没有边相连则初始化为0 uN是匹配左边的顶点数,vN是匹配右边的顶点数 调用:res=hungary();输出最大匹配数 优点:适用于稠密图,DFS找增广路,实现简洁易于理解 时间复杂度:O(VE) ***************************************************************************/

HDU1151_Air Raid(二分图/最小路径覆盖=n-最大匹配)

解题报告 题目传送门 题意: 一个小镇,所有的街道都是单向的,这些街道都是从一个十字路口通往另一个十字路口,已知从任何十字路口出发,沿着这些街道行走,都不能回到同一个十字路口,也就是说不存在回路. 计算攻击这个小镇需要派的伞兵最少数目,这些伞兵要走遍小镇的所有十字路口,每个十字路口只由一个伞兵走到.每个伞兵在一个十字路口着陆,沿着街道可以走到其他十字路口. 思路: 用最小的伞兵覆盖街道,最小路径覆盖模型.把每个点拆成X1,Y1,这样建成二分图.最小路径覆盖=n-最大匹配数. #include <

HDOJ1151有向图最小路径覆盖

//有向图最小路径覆盖:从某一点出发沿着有向路径,不走回路,能将所有的结点遍历. #include<iostream> #include<cstdio> #include<vector> #include<set> using namespace std; const int MAX_N=125; int match[MAX_N]; bool vis[MAX_N]; set<int> insert; vector<int> e[MAX

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 最小路径覆盖 == 顶点数 - 最大匹配. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define N 130 int maps[N][N], vis[N], used[N], n, m, ans; bo

POJ 1422 Air Raid (二分图最小点集覆盖 匈牙利算法)

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