hdu1151 Air Raid 基础匈牙利

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 #define MAXN 150
 8 int n;    //十字路口的数量
 9 int m;    //路的个数
10 int map[MAXN][MAXN];
11 int x[MAXN], y[MAXN];
12 int mark[MAXN];
13
14 int search(int a)
15 {
16     for (int i = 1; i <= n; i++)
17     {
18         if (map[a][i] && !mark[i])
19         {
20             mark[i] = 1;
21             if (y[i] == 0 || search(y[i]))
22             {
23                 x[a] = i;
24                 y[i] = a;
25                 return a;
26             }
27         }
28     }
29     return 0;
30 }
31
32 int main()
33 {
34     int t;
35     scanf("%d", &t);
36     while (t--)
37     {
38         memset(map, 0, sizeof(map));
39         memset(mark, 0, sizeof(mark));
40         scanf("%d %d", &n, &m);
41         for (int i = 0; i < m; i++)
42         {
43             int a, b;
44             scanf("%d %d", &a, &b);
45             if (map[a][b] == 0)
46                 map[a][b] = 1;
47         }
48
49         int ans1 = 0;
50         memset(x, 0, sizeof(x));
51         memset(y, 0, sizeof(y));
52
53         for (int i = 1; i <= n; i++)
54         {
55             if (x[i] == 0)
56             {
57                 memset(mark, 0, sizeof(mark));
58                 if (search(i))
59                     ans1++;
60             }
61         }
62         printf("%d\n", n - ans1);
63     }
64     //system("pause");
65     return 0;
66 }
时间: 2024-10-14 08:58:04

hdu1151 Air Raid 基础匈牙利的相关文章

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

hdoj 1151 Air Raid【匈牙利算法+二分最小顶点覆盖】

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

HDU1151 Air Raid(有向图最小路径覆盖)

题意: N个点M条边的有向图 意思就是问最小覆盖 思路: 有向图建单向边,然后匈牙利求最大匹配数 用N-最大匹配就可以了 /* *********************************************** Author :devil Created Time :2016/5/17 11:55:14 ************************************************ */ #include <cstdio> #include <cstring

hdu1151 Air Raid

http://acm.hdu.edu.cn/showproblem.php?pid=1151 增广路的变种2:DAG图的最小路径覆盖=定点数-最大匹配数 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<math.h> 6 using namespace std; 7 const int N=510; 8 in

hdu1151 Air Raid,DAG图的最小路径覆盖

点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 150; int g[maxn][maxn]; int n, m; int link[maxn]; bool used[

Air Raid[HDU1151]

Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4974 Accepted Submission(s): 3347 Problem DescriptionConsider a town where all the streets are one-way and each street leads from one interse

POJ 1422 Air Raid (二分图最小点集覆盖 匈牙利算法)

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

HDU1151:Air Raid(最小边覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6528    Accepted Submission(s): 4330 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 Description: Consider a town where all the stree

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