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

题目链接

题意:给定一个有向图,问最少放多少个伞兵,使得所有路口都能被走到,所有路口有且只被走到一次。

思路:二分图的最小路径覆盖。

在一个 N*N 的有向图中,路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联;(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次);如果不考虑图中存在回路,那么每每条路径就是一个弱连通子集.

由上面可以得出:

1.一个单独的顶点是一条路径;

2.如果存在一路径 p1,p2,......pk,其中 p1 为起点,pk 为终点,那么在覆盖图中,顶点 p1,p2,......pk 不再与其它的顶点之间存在有向边.

最小路径覆盖就是找出最小的路径条数,使之成为 G 的一个路径覆盖.

路径覆盖与二分图匹配的关系:最小路径覆盖=|G|-最大匹配数;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 1005;

vector<int> g[MAXN];
int linker[MAXN];
bool used[MAXN];
int n, m;

bool dfs(int u) {
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!used[v]) {
            used[v] = true;
            if (linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}

int save[MAXN];

int hungary() {
    int res = 0;
    memset(linker, -1, sizeof(linker));
    for (int u = 1; u <= n; u++) {
        memset(used, false, sizeof(used));
        if (dfs(u)) res++;
    }
    return res;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) g[i].clear();

        int u, v;
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
        }
        printf("%d\n", n - hungary());
    }
    return 0;
}

时间: 2024-10-11 15:58:11

POJ1422-Air Raid(最小路径覆盖)的相关文章

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

给一个DAG图,一个人可以走一条路,或者就在一个点(路径长度为0),问至少需要多少人可以覆盖所有点. 根据二分图的性质: DAG的最小路径覆盖,将每个点拆点后求最大匹配数m,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j',j→k',k→l'....构成一条有向路径. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algori

【网络流24题----02】Air Raid最小路径覆盖

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

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

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

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把所有的顶点都覆盖)

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

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

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

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

[POJ] 1422 Air Raid(最小路径覆盖)

题目地址:http://poj.org/problem?id=1422 一个地图上有n个小镇,以及连接着其中两个小镇的有向边,而且这些边无法形成回路.现在选择一些小镇空降士兵(1个小镇最多1个士兵),士兵能沿着边走到尽头,问最少空降几个士兵,能遍历完所有的小镇.最小路径覆盖问题.先拆点,将每个点分为两个点,左边是1到n个点,右边是1到n个点然后每一条有向边对应左边的点指向右边的点 因为最小路径覆盖 = 节点数 - 最大匹配数,所以匈牙利算法求一下二分图最大匹配就能得出结果了. 1 #includ

Air Raid(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 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 Air Raid 最少路径覆盖

题目链接:http://poj.org/problem?id=1422 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 t