POJ Air Raid 【DAG的最小不相交路径覆盖】
传送门:http://poj.org/problem?id=1422
Air Raid
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9303 | Accepted: 5570 |
Description
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
Input
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
Output
Sample Input
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3
Sample Output
2
1
Source
题意概括:
N个路口 M 条单向路,安排若干个士兵,这些士兵只会沿着当前所在结点路径走,问最少安排多少个士兵就可以把所有路口巡逻完毕。
解题思路:
拆点:
把原图的每个点V拆成VxVx和VyVy两个点,如果有一条有向边A->B,那么就加边Ax−>By。这样就得到了一个二分图。
最小路径覆盖=原图的结点数-新图的最大匹配数。
一开始每个点都是独立的为一条路径,总共有n条不相交路径。我们每次在二分图里找一条匹配边就相当于把两条路径合成了一条路径,也就相当于路径数减少了1。所以找到了几条匹配边,路径数就减少了多少。所以有最小路径覆盖=原图的结点数-新图的最大匹配数。
AC code(静态邻接表):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
struct Edge
{
int v, nxt;
}edge[MAXN]; int head[MAXN*MAXN], cnt;
int linker[MAXN];
bool used[MAXN];
int N, M; void init()
{
memset(head, -, sizeof(head));
memset(linker, -, sizeof(linker));
cnt = ;
} void add(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} bool Find(int x)
{
int v;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(used[v]) continue;
used[v] = true;
if(linker[v] == - || Find(linker[v])){
linker[v] = x;
return true;
}
}
return false;
} int main()
{
int T_case, u, v, ans;
scanf("%d", &T_case);
while(T_case--){
init();
scanf("%d %d", &N, &M);
while(M--){
scanf("%d%d", &u, &v);
add(u, v);
}
ans = ;
for(int i = ; i <= N; i++){
memset(used, , sizeof(used));
if(Find(i)) ans++;
}
printf("%d\n", N-ans);
}
return ;
}
POJ Air Raid 【DAG的最小不相交路径覆盖】的更多相关文章
- Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】
Consider a town where all the streets are one-way and each street leads from one intersection to ano ...
- P2172 [国家集训队]部落战争 二分图最小不相交路径覆盖
二分图最小不相交路径覆盖 #include<bits/stdc++.h> using namespace std; ; ; ; ], nxt[MAXM << ], f[MAXM ...
- [luoguP2765] 魔术球问题(最大流—最小不相交路径覆盖)
传送门 枚举球的个数 num 如果 i < j && (i + j) 是完全平方数,那么 i -> j' 连一条边 再加一个超级源点 s,s -> i 再加一个超级汇 ...
- POJ1422Air Raid(二分图,最小不相交路径覆盖)
Air Raid Consider a town where all the streets are one-way and each street leads from one intersecti ...
- POJ 2594 Treasure Exploration 最小可相交路径覆盖
最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...
- HDU 4862 Jump(最小K路径覆盖)
输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...
- 网络费用流-最小k路径覆盖
多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- POJ 3308 Paratroopers (对数转换+最小点权覆盖)
题意 敌人侵略r*c的地图.为了消灭敌人,可以在某一行或者某一列安置超级大炮.每一个大炮可以瞬间消灭这一行(或者列)的敌人.安装消灭第i行的大炮消费是ri.安装消灭第j行的大炮消费是ci现在有n个敌人 ...
- POJ - 2125 Destroying The Graph (最小点权覆盖)
题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...
随机推荐
- 01-struts2配置详解
1 struts.xml配置详解 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE 的理解
[2013-12-06 11:06:21,715] [C3P0PooledConnectionPoolManager[identityToken->2tl0n98y1iwg7cbdzzq7a|7 ...
- [转]How to use IHttpContextAccessor in static class to set cookies
本文转自:http://stackoverflow.com/questions/37329354/how-to-use-ihttpcontextaccessor-in-static-class-to- ...
- Win2D 官方文章系列翻译 - 预乘 Alpha
本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-premultiplied-alpha/ 在计算机绘图中有两种表示颜色值不透明度的方法.Win2D 中两种方法 ...
- 上传文件插件-bootstrap-fileinput
1. js文件: <link href="/bootstrap/css/fileinput.css" media="all" rel="styl ...
- tornado基本使用【服务】
1.安装 2.请求处理程序和请求参数 1.安装 pip install tornado 2.请求处理程序和请求参数 Tornado 的 Web 程序会将 URL 或者 URL 范式映射到 tornad ...
- JavaScript数组常用操作总结
我们在日常开发过程中,使用到原生 JavaScript的时候,有时候会频繁的对数组进行操作,今天我把工作以来,经常用到的有关 JavaScript数组的方法总结一下,方便日后工作的时候查找使用! 一. ...
- 04.Continue,和三元表达式的学习
立即结束本次循环,判断循环条件,如果成立,则进入下一次循环,否则退出循环. 举例:运动员跑步喝水的例子 比如:我编写代码的时候,上个厕所,回来继续写代码 练习1: namespace _09.练习02 ...
- mac解决系统设置安全与隐私没有允许所有来源
解决系统设置安全与隐私没有允许所有来源:sudo spctl --master-disable
- Redis简介及持久化
Redis是一个key-value数据库,他不仅性能强劲,而且还具有复制特性以及为解决问题而生的独一无二的数据模型.可用于缓存.消息队列.发布.订阅消息.商品列表和评论列表等场景.Redis提供了5种 ...