POJ Air Raid 【DAG的最小不相交路径覆盖】
传送门:http://poj.org/problem?id=1422
Air Raid
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9303 | Accepted: 5570 |
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's streets you can never reach the same intersection i.e. the town's streets form no cycles.
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.
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
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:
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.
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
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.
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(静态邻接表):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #define INF 0x3f3f3f3f 6 using namespace std; 7 const int MAXN = 150; 8 struct Edge 9 { 10 int v, nxt; 11 }edge[MAXN]; 12 13 int head[MAXN*MAXN], cnt; 14 int linker[MAXN]; 15 bool used[MAXN]; 16 int N, M; 17 18 void init() 19 { 20 memset(head, -1, sizeof(head)); 21 memset(linker, -1, sizeof(linker)); 22 cnt = 0; 23 } 24 25 void add(int from, int to) 26 { 27 edge[cnt].v = to; 28 edge[cnt].nxt = head[from]; 29 head[from] = cnt++; 30 } 31 32 bool Find(int x) 33 { 34 int v; 35 for(int i = head[x]; i != -1; i = edge[i].nxt){ 36 v = edge[i].v; 37 if(used[v]) continue; 38 used[v] = true; 39 if(linker[v] == -1 || Find(linker[v])){ 40 linker[v] = x; 41 return true; 42 } 43 } 44 return false; 45 } 46 47 int main() 48 { 49 int T_case, u, v, ans; 50 scanf("%d", &T_case); 51 while(T_case--){ 52 init(); 53 scanf("%d %d", &N, &M); 54 while(M--){ 55 scanf("%d%d", &u, &v); 56 add(u, v); 57 } 58 ans = 0; 59 for(int i = 1; i <= N; i++){ 60 memset(used, 0, sizeof(used)); 61 if(Find(i)) ans++; 62 } 63 printf("%d\n", N-ans); 64 } 65 return 0; 66 }