[POJ] 1422 Air Raid(最小路径覆盖)

题目地址:http://poj.org/problem?id=1422

一个地图上有n个小镇,以及连接着其中两个小镇的有向边,而且这些边无法形成回路。现在选择一些小镇空降士兵(1个小镇最多1个士兵),士兵能沿着边走到尽头,问最少空降几个士兵,能遍历完所有的小镇。最小路径覆盖问题。先拆点,将每个点分为两个点,左边是1到n个点,右边是1到n个点
然后每一条有向边对应左边的点指向右边的点

因为最小路径覆盖 = 节点数 - 最大匹配数,所以匈牙利算法求一下二分图最大匹配就能得出结果了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<math.h>
 6 #include<stdbool.h>
 7 #include<time.h>
 8 #include<stdlib.h>
 9 #include<set>
10 #include<map>
11 #include<stack>
12 #include<queue>
13 #include<vector>
14 using namespace std;
15 #define clr(x,y)    memset(x,y,sizeof(x))
16 #define sqr(x)      ((x)*(x))
17 #define rep(i,a,b)  for(int i=(a);i<=(b);i++)
18 #define LL          long long
19 #define INF         0x3f3f3f3f
20 #define A           first
21 #define B           second
22 #define PI          3.14159265358979323
23 const int N=200+11;
24 int n,m,f[N],g[N][N],link[N];
25
26 void init()
27 {
28     clr(f,0);
29     clr(g,0);
30     clr(link,-1);
31 }
32
33 bool find(int x)
34 {
35     for(int i=1;i<=n;i++) {
36         if(!f[i] && g[x][i]) {
37             f[i]=1;
38             if(link[i]==-1 || find(link[i])) {
39                 link[i]=x;
40                 return true;
41             }
42         }
43     }
44
45     return false;
46 }
47
48 int hungary()
49 {
50     int ans=0;
51     for(int i=1;i<=n;i++) {
52         clr(f,0);
53         if(find(i)) ans++;
54     }
55     return ans;
56 }
57
58 int main()
59 {
60     int u,v,cas,k;
61
62     scanf("%d",&cas);
63     while(cas--) {
64         init();
65         scanf("%d%d",&n,&m);
66         while(m--) {
67             scanf("%d%d",&u,&v);
68             g[u][v]=1;
69         }
70         printf("%d\n",n-hungary());
71     }
72     return 0;
73 }
时间: 2024-12-07 16:01:15

[POJ] 1422 Air Raid(最小路径覆盖)的相关文章

poj 1422 Air Raid 最少路径覆盖

题目链接:http://poj.org/problem?id=1422 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 t

hdu1151 Air Raid --- 最小路径覆盖

给一个DAG图,一个人可以走一条路,或者就在一个点(路径长度为0),问至少需要多少人可以覆盖所有点. 根据二分图的性质: DAG的最小路径覆盖,将每个点拆点后求最大匹配数m,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j',j→k',k→l'....构成一条有向路径. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algori

【网络流24题----02】Air Raid最小路径覆盖

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4591    Accepted Submission(s): 3072 Problem Description Consider a town where all the streets are one-way and each street leads from one

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 102   Problem Description Consider a town where all the streets are one-way and each street leads from one

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把所有的顶点都覆盖)

题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 102   Problem Description Consider a town where all the streets are one-way and each street leads from one

POJ 1422 二分图(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 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 i

hdu 1151 Air Raid(最小路径覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3894    Accepted Submission(s): 2573 Problem Description Consider a town where all the streets are one-way and each street leads from on

hdu 1151 Air Raid 最小路径覆盖

Air Raid Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1151 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

Air Raid(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 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 i