POJ1422 最小路径覆盖入门
来源:互联网 发布:类似matlab的软件 编辑:程序博客网 时间:2024/06/05 18:45
题意:DAG求最小路径覆盖。
注意:二分匹配只试用于求DAG的最小路径覆盖, 有环就不行,具体可以理解证明。
对n个点进行拆点,分成左右两排点,对于边<u, v> 建 <u', v''> 。
然后 最小路径覆盖 == 总点数n - 最大匹配。
简单的证明: 每匹配一对<u,v>就说明u和v在同一条路径上,拿路径数就少1。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn = 130;vector <int> edge[maxn];int n, m;int pre[maxn];bool vis[maxn];bool dfs(int u) {for(int i = 0; i < (int) edge[u].size(); i++) {int v = edge[u][i];if(vis[v]) continue;vis[v] = 1;if(pre[v] == -1 || dfs(pre[v])) {pre[v] = u;return 1;}}return 0;}int main() {int i, cas;scanf("%d", &cas);while(cas--) {scanf("%d%d", &n, &m);int x, y;for(i = 1; i <= n; i++)edge[i].clear();while(m--) {scanf("%d%d", &x, &y);edge[x].push_back(y);}memset(pre, -1, sizeof(pre));int cnt = 0;for(i = 1; i <= n; i++) {memset(vis, 0, sizeof(vis));cnt += dfs(i);}printf("%d\n", n-cnt);}return 0;}
- POJ1422 最小路径覆盖入门
- poj1422最小路径覆盖
- poj1422最小路径覆盖
- POJ1422 最小路径覆盖
- 最小路径覆盖 POJ2594 POJ1422
- poj1422 zoj1525 Air Raid 最小路径覆盖
- POJ1422 Air Raid 【DAG最小路径覆盖】
- POJ1422-Air Raid(最小路径覆盖)
- poj1422(二分图,最小路径覆盖)
- 最小路径覆盖 poj1422 Air Raid
- Air Raid POJ1422 & HDU1151 最小路径覆盖
- 最小路径覆盖问题poj2594&&poj1422(匈牙利解法)
- hdu1151 poj1422 最小路径覆盖.最大二分匹配
- POJ1422 Air Raid【二分图最小路径覆盖】
- poj1422 Air Raid 最小路径覆盖=顶点数-最大匹配数
- POJ1422——Air Raid(二分图,最小路径覆盖)
- poj1422-最小路径覆盖(不交叉)&二分图-Air Raid
- POJ1422 有向无环图求最小点覆盖[模板]
- LEADTOOLS v18多平台条码识别(代码示例)
- SharedPreferences存储数据 基本学习实例
- Spring之IOC
- android Linux内核启动
- 解决eclipse中overlaps the location of another project: 'xxxx'
- POJ1422 最小路径覆盖入门
- com.opensymphony.module.sitemesh.filter.PageFilter页面装饰器
- 华为应聘机试题 “操作系统调度”
- Android事件传递机制【按键事件】
- PHP字符编码问题
- Creating a Static Library in iOS Tutorial
- tomcat发布web service教程
- 内存泄露检查
- 学习NFC