hdu1151+poj2594(最小路径覆盖)

传送门:hdu1151 Air Raid

题意:在一个城镇,有m个路口,和n条路,这些路都是单向的,而且路不会形成环,现在要弄一些伞兵去巡查这个城镇,伞兵只能沿着路的方向走,问最少需要多少伞兵才能把所有的路口搜一遍。

分析:有向无环图不相交最小路径覆盖数,等于节点数减去二分图的最大匹配数,对于每条弧,弧头作为X部,弧尾作为Y部。最后在求得最大匹配的基础上,没有被匹配的Y部的点就是简单路径的起点。其个数刚好就是节点数减去二分图的最大匹配数。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 200
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
int match[N],vis[N],n,m;
vector<int>g[N];
int dfs(int u)
{
    for(int i=0,sz=g[u].size();i<sz;i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(match[v]==-1||dfs(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    FILL(match,-1);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        FILL(vis,0);
        if(dfs(i))ans++;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            g[i].clear();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
        }
        int res=hungary();
        printf("%d\n",n-res);
    }
}

传送门:poj2594 Treasure Exploration

题意:在一个有向图上,至少放多少个机器人可以遍历整个图(每个顶点可以重复遍历)。

分析:有向无环图可相交最小路径覆盖数,得先跑一遍Floyd,然后再进行最大匹配。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 510
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
int match[N],vis[N],n,m;
int g[N][N],mat[N][N];
char s[N][N];
int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&g[u][i])
        {
            vis[i]=1;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    FILL(match,-1);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        FILL(vis,0);
        if(dfs(i))ans++;
    }
    return ans;
}
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]=1;
        }
}
int main()
{
    while(scanf("%d%d",&n,&m)>0)
    {
        if(n+m==0)break;
        FILL(g,0);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u][v]=1;
        }
        floyd();
        int res=hungary();
        printf("%d\n",n-res);
    }
}

时间: 2024-08-26 14:18:31

hdu1151+poj2594(最小路径覆盖)的相关文章

hdu1151 poj1422 最小路径覆盖.最大二分匹配

Air RaidTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 1422 Appoint description: Description Consider a town where all the streets are one-way and each street leads from one intersection to another.

【二分图匹配入门专题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

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

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'

hdu1151 Air Raid,DAG图的最小路径覆盖

点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 150; int g[maxn][maxn]; int n, m; int link[maxn]; bool used[

poj2594 (最小路径覆盖 + floyd)

题目链接 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析: 这时最小路径覆盖问题, 最小路径覆盖 = |V| - 最大匹配数. (有关最小路径覆盖,最大匹配问题,相关概念不懂得点这里) 当然做这道题还有一个坑!! 如果有向图的边有相交的情况,那么就不能简单的对原图求二分匹配了 详细讲解看这 #include<iostream> #include<cstdio> #include<algorithm&

POJ2594 Treasure Exploration【二分图最小路径覆盖】【Floyd】

题目链接: http://poj.org/problem?id=2594 题目大意: 给你N个地点,M条有向边,已知构成的图是有向无环图.现在要在地点上放机器人通过M 条边来遍历N个地点,问:最少需要多少个机器人可以遍历N个地点. 思路: 这是一道求最小路径覆盖的题目.和一般最小路径覆盖的题目不一样的地方是:这里的点可 以重复遍历.也就是可以有两个及以上的机器人经过同一个点. 那么,先建立一个二分图, 两边都为N个地点.然后在原图的基础上,用Floyd求一次传递闭包,也就是如果点i可以到达 点j

POJ2594 Treasure Exploration(最小路径覆盖+传递闭包)

题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上, 再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.点可以重复去. 思路: 最小路径覆盖,只是点可以重复去,就需要求传递闭包,用floyd /* *********************************************** Author :devil Created Time :2016/5/17 16:45:13 *****************************

POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9794   Accepted: 3975 题目链接:http://poj.org/problem?id=2594 Description: Have you ever read any book about treasure exploration? Have you ever see any film about treasure

有向无环图(DAG)的最小路径覆盖

DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如图,其最小路径覆盖数为3.即1->3>4,2,5. 最小可相交路径覆盖:每一条路径经过的顶点可以相同.如果其最小路径覆盖数为2.即1->3->4,2->3>5. 特别的,每个点自己也可以称为是路径覆盖,只不过路径的长度是0. DAG的最小不相交路径覆盖 算法:把原图的每个点