POJ 2594 浅谈可相交的二分图DAG最小路径覆盖

这里写图片描述
世界真的很大
最小路径覆盖的问题还算讨论过比较多次了
这个算是最小路径覆盖问题的一个变种,额外处理DAG中可以相交的情况
所以说啊,图论的东西的变形和建模实在是太多了。。

看题先:

description:

求DAG的最小可相交路径覆盖

input:

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

output:

For each test of the input, print a line containing the least robots needed.

对于普通的“不可相交的”最小路径覆盖,我们的方法是使得每一个点额外建一个虚点,然后映射其连边。
假设一开始有n条路径(n个点),每有一个匹配就相当于可以把两个点连在一起,就会少一条边,求得的最大匹配就是最多可以少多少条边。。

可以这样做的基本原理是,每一条路径是不相交的,就是说每一个点只会属于一条路径,能为答案提供的贡献最多也只有-1
但是如果是“可以相交”就不同了,一个点可以同时在多条路径上,即可以为多条路径提供贡献
二分图能处理的匹配最大也只有1,这没有办法

那我们只能转化思路,可以看一下这篇博客
感性理解一下的话,大概就是把本来不能直接连在一起的点连在一起,把交点的贡献分散化,分散到另外两个一开始不连在一起的点上

作为结论则需要记住可相交的二分图DAG最小路径覆盖等于闭包传递补全后的图的不可交最大路径匹配

完整代码:

#include<stdio.h>
#include<cstring>
using namespace std;

struct edge
{
    int v,last;
}ed[400010];

int n,m,ans=0,tot=0,num=0;
int head[20010],book[20010],match[20010],mp[510][510];

void add(int u,int v)
{
    num++;
    ed[num].v=v;
    ed[num].last=head[u];
    head[u]=num;
}

int dfs(int u)
{
    for(int i=head[u];i;i=ed[i].last)
    {
        int v=ed[i].v;
        if(book[v]!=tot)
        {
            book[v]=tot;
            if(!match[v] || dfs(match[v]))
            {
                match[u]=v,match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

void init()
{
    memset(book,0,sizeof(book));
    memset(match,0,sizeof(match));
    memset(head,0,sizeof(head));
    memset(mp,0,sizeof(mp));
    num=tot=ans=0;
}

int main()
{
    while(1)
    {
        init();
        scanf("%d%d",&n,&m);
        if(!n) break ;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            mp[u][v]=1;
        }
        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(mp[i][k] && mp[k][j]) mp[i][j]=1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                if(i==j || !mp[i][j]) continue ;
                add(i+n,j),add(j,i+n);
            }
        for(int i=1;i<=2*n;i++)
        {
            tot++;
            if(!match[i]) ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
/*
EL PSY CONGROO
*/

嗯,就是这样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值