POJ 2594 浅谈可相交的二分图DAG最小路径覆盖
来源:互联网 发布:统计学数据分析案例题 编辑:程序博客网 时间:2024/06/07 21:45
世界真的很大
最小路径覆盖的问题还算讨论过比较多次了
这个算是最小路径覆盖问题的一个变种,额外处理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*/
嗯,就是这样
- POJ 2594 浅谈可相交的二分图DAG最小路径覆盖
- POJ Treasure Exploration(DAG最小可相交路径覆盖)
- poj 2594(可相交的最小路径覆盖)
- POJ 2594 可相交的最小路径覆盖
- poj 2594 Treasure Exploration 可重复覆盖的二分图最小覆盖路径
- Poj 2594 Treasure Exploration【可相交最小路径覆盖】
- BZOJ 2150 浅谈二分图Bipartite Graph及DAG最小路径覆盖
- POJ 2594 二分图最小路径覆盖
- POJ 2594 允许有相交点的最小路径覆盖
- DAG最小路径覆盖与二分图最大匹配
- DAG的最小路径覆盖和二分图的最大匹配
- DAG的最小路径覆盖和二分图的最大匹配(hdu1151+hdu3861)
- DAG的最小路径覆盖和二分图的最大匹配
- poj 1422 Air Raid (二分图+有向图最小不相交路径覆盖)
- HDU 1151 二分图的DAG最小路径覆盖 解题报告
- POJ 1548 Robots(DAG最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- 记推送过程踩坑
- HMM之后向算法
- 指针数组和数组指针
- 批量替换 MySQL 指定字段中的字符串
- 【LeetCode】C# 34、Search for a Range
- POJ 2594 浅谈可相交的二分图DAG最小路径覆盖
- memcache的使用
- 关于Ubuntu下MySQL的设置问题
- C#编码规范
- This application has no explicit mapping for /error, so you are seeing this as a fallback
- Android 7.0系统 PopupWindow的bug
- python中的默认参数self
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
- 结合ZXing实现类似微信扫二维码放大摄像头