--hdu 1151 - > 双向路径搜索解决覆盖问题

http://acm.hdu.edu.cn/showproblem.php?pid=1151

伞兵可以降落到图上的任意一个点,用最少的伞兵在单向道路上走完所有的点

解决:对一个点双向dfs,目的在于找到任意一个其他伞兵未访问的非起点,只要找到这个点,则可以保证这条路径是最佳的;

因为路是单向的,所以从这个点出发后,就不会再次回到这个点(题目中保证没有环)。

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

int ms[125][125];
int vis[125];
int n,m;

bool dfs(int u){

    bool wala = false;

    for(int i = 1;i<=n;i++){

        if(vis[i] == 0 &&ms[u][i]){

            vis[i] = 1;
            dfs(i);
            wala = true;
            break;

        }else if(ms[u][i] && dfs(i)){

            wala = true;
            break;

        }

    }
    return wala;
}

bool re_dfs(int u){

    bool wala = false;

    for(int i = 1;i<=n;i++){

        if(vis[i] == 0&&ms[i][u]){

            vis[i] = 1;
            re_dfs(i);
            wala = true;
            break;

        }else if(ms[i][u] &&re_dfs(i)){

            wala = true;
            break;

        }

    }
    return wala;
}

int main(){

     int t ;

     scanf("%d",&t);

     while(t--){

        int ans = 0;

        memset(ms,0,sizeof(ms));
        memset(vis,0,sizeof(vis));

        scanf("%d%d",&n,&m);

        int u,v;

        for(int i = 0;i<m;i++){

            scanf("%d%d",&u,&v);

            ms[u][v] = 1;

        }

        for(int  i = 1;i <= n;i++){

            if(!vis[i]){

                vis[i] = 1;
                dfs(i);
                re_dfs(i);
                ans ++;

            }

        }

        printf("%d\n",ans);

     }

}
时间: 2024-11-10 19:42:04

--hdu 1151 - > 双向路径搜索解决覆盖问题的相关文章

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

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

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 hdu 3335

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

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

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

HDU 3111 Sudoku(精确覆盖)

数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 #include <string> 7 #includ

hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版

//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 1 //hdu 1150 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue>

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

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