poj 2594(最小路径覆盖)

题意:有n个点需要探索(编号从1到n),然后给出图上有m条有向边,问最少选择多少个点能遍历所有的点。注意走过的点可以重复再走。

题解:二分图求最小路径覆盖问题中点是不可以重复的,但这道题说可以重复,所以做法是可以把走过的点跳过,间接相连的点改为直接相连就可以跳过了。

特殊样例

5 4

1 2

2 3

4 2

2 5

#include <stdio.h>
#include <string.h>
const int N = 505;
int n, m, g[N][N], vis[N], link[N];

bool dfs(int u) {
    for (int i = 1; i <= n; i++) {
        if (!vis[i] && g[u][i]) {
            vis[i] = 1;
            if (link[i] == -1 || dfs(link[i])) {
                link[i] = u;
                return true;
            }
        }
    }
    return false;
}

int main() {
    while (scanf("%d%d", &n, &m) == 2 && n + m) {
        memset(g, 0, sizeof(g));
        memset(link, -1, sizeof(link));
        int a, b;
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &a, &b);
            g[a][b] = 1;
        }
        for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    g[i][j] |= (g[i][k] & g[k][j]);
        int res = 0;
        for (int i = 1; i <= n; i++) {
            memset(vis, 0, sizeof(vis));
            if (dfs(i))
                res++;
        }
        printf("%d\n", n - res);
    }
    return 0;
}
时间: 2024-07-30 03:52:20

poj 2594(最小路径覆盖)的相关文章

POJ 3216 最小路径覆盖+floyd

Repairing Company Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6646   Accepted: 1788 Description Lily runs a repairing company that services the Q blocks in the city. One day the company receives M repair tasks, the ith of which occu

POJ 2594 Treasure Exploration(最小路径覆盖变形)

POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要在多一步.利用floyd求出传递闭包.然后依据这个新的图去做最小路径覆盖就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using names

POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

POJ 2594 传递闭包的最小路径覆盖

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7171   Accepted: 2930 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

POJ 2594 Treasure Exploration【传递闭包+最小路径覆盖】

大意: 有n个点,告诉你一些单向边,问多少条边能把所有的点覆盖[注意点能重复覆盖  比如4->1->2   5->3] 分析: 知识储备: 传递闭包:  所谓传递,可以这么理解,对于节点j如果i能到k并且k能到j那么i能到j,这样用像floyed就能处理出任意两个点能否到达 for(int k = 1; k <= n; k++) { for(int i = 1; i <= n; i++) { if(W[i][k]) { for(int j = 1; j <= n; j+

poj 2594 Treasure Exploration(最小路径覆盖,可重点)

题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径覆盖了.我来大概解释一下为什么是对的,首先我们要明确,当我们重复利用一个点的时候,一定是有两个比较良好的路径相交了,而二分图是不允许这样的情况存在的,因为那必然存在了一个点有一个以上的出度或者入度了,而怎么避免这个问题呢,看下面的图: 这就是针对这个问题的一个典型的模型,如果使用正常二分图,求得的匹

POJ 2594 二分图最小路径覆盖

点击打开链接 题意:将所有点都连起来至少需要多少条路径 思路:二分图的最小路径覆盖,而最小路径覆==图的顶点数-图的最大匹配,而当初还学习过最小顶点覆盖==最大匹配,而最小顶点覆盖需要连双向边,结果除以2,那是因为1-->2时,点1和点2都已经用过,所以我在连一个相应的一条边,代表这两个点不能在用了,样例详见hdu 1054 第二组.而接下来的求最小路径覆盖的最大匹配我们就只能是单向的,这个为什么可以避免呢,因为1-->2-->3这样的话,最小路径为1,但是转化为二分图上的话,对应的点2

POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)

题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和m, 代表有n个顶点, m条边.(m和n同时为0时则输入数据结束) 接下来m行,每行两个数字 a, b代表 从a到b可以通行. 题目分析: 这道题目与最小路径有一点差别,最小路径覆盖上是不存在交叉路的,但是这个题目是存在交叉路的. 对于交叉路的处理我们可以使用Floyd闭包传递.即 i->j, j->

poj 2594 Treasure Exploration(最小路径覆盖/二分最大匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2944 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored