【最小点覆盖】POJ3041-Asteroids

【题目大意】

在n*n的网格上有n个点,每次删除一行或者一列,问至少要删除几次才能删除完全部的这些店?

【思路】

在国庆最后一天到来前,把二分图的三个基本情况【最小点覆盖】【DAG图的最小路径覆盖】和【二分图的最大独立集】全部复习了一遍。

这道题是非常典型的最小点覆盖,指的是用最少的点让每条边都至少和两个集合中的某一个点关联。

最小点覆盖=二分图最大匹配数。

对于这道题而言,我们把横坐标作为集合X,纵坐标作为集合Y,对于点(x,y),由X中的x连向Y中的y。对于每条边,只要x和y中有一个被删除即可,明显的最小点覆盖模型。

十分钟水~

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 const int MAXN=505;
 8 vector<int> E[MAXN];
 9 int lk[MAXN],vis[MAXN],n,k;
10
11 int find(int u)
12 {
13     for (int i=0;i<E[u].size();i++)
14     {
15         int v=E[u][i];
16         if (!vis[v])
17         {
18             vis[v]=1;
19             if (!lk[v]||find(lk[v]))
20             {
21                 lk[v]=u;
22                 return 1;
23             }
24         }
25     }
26     return 0;
27 }
28
29 void init()
30 {
31     scanf("%d%d",&n,&k);
32     for (int i=1;i<=k;i++)
33     {
34         int x,y;
35         scanf("%d%d",&x,&y);
36         E[x].push_back(y);
37     }
38 }
39
40 void solve()
41 {
42     int ans=0;
43     memset(lk,0,sizeof(lk));
44     for (int i=1;i<=n;i++)
45     {
46         memset(vis,0,sizeof(vis));
47         if (find(i)) ans++;
48     }
49     printf("%d",ans);
50 }
51
52 int main()
53 {
54     init();
55     solve();
56     return 0;
57 }
时间: 2024-12-16 16:01:17

【最小点覆盖】POJ3041-Asteroids的相关文章

poj3041 Asteroids --- 最小点覆盖

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<queue> const int maxn=510; using namespace std; int my[maxn],mx[maxn],vis[maxn],e[maxn][maxn],n;

POJ3041 Asteroids(二分图最小点覆盖)

Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the

POJ3041 Asteroids【二分图最小点覆盖】

题目链接: http://poj.org/problem?id=3041 题目大意: 有一个N*N的矩阵,有些格子上有障碍物(坐标为(x,y) ),在消除这些障碍物的时候,可以一次性消除 该障碍物同一行所有的障碍物,或是一次性消除该障碍物同一列所有的障碍物.只能选择清理该行或是 清理该列.问:最小进行多少次消除,就可以清理所有的障碍物. 思路: 可以将每一行当做一个点,这样总共有N个点,作为二分图的一边.将每一列当做一个点,这样又有N 个点,作为二分图的另一边.将有障碍物的行点和列点连接起来,每

[POJ3041] Asteroids(最小点覆盖-匈牙利算法)

传送门 题意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍,最少要几次. 解析: 把每一行与每一列当做二分图两边的点. 某格子有障碍,则对应行与列连边. 选出最少的点,使得所有边被覆盖. 最小点覆盖. ——代码 1 #include <cstdio> 2 #include <cstring> 3 #define M(x, a) memset(a, x, sizeof(a)) 4 5 using namespace std; 6 7 cons

Asteroids POJ - 3041 二分图最小点覆盖

Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice point

poj 3041 Asteroids(最小点覆盖)

http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17543   Accepted: 9548 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <

POJ 3041 Asteroids (图论-最小点覆盖)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15079   Accepted: 8240 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

POJ 3401 Asteroids 求最小点覆盖集

把行和列都看做是点,小行星看成是边的话,那么这个显然就是求一个最小点覆盖集的问题. 最小点覆盖 == 最大匹配 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #inc

Asteroids POJ - 3041 【最小点覆盖集】

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortun

pku 3041 Asteroids(最小点覆盖)

题意: N*N的矩阵,有K个敌人,坐标分别是(C1,C1),.....,(Rk,Ck). 有一个武器,每发射一次,可消掉某行或某列上的所有的敌人. 问消灭所有敌人最少需要多少发. 思路: 二分建图:左边N个点代表行号,右边N个点代表列号.如果第i行第j列上有敌人,则将左边点i和右边点j连一条线. 则转化为求此二分图的最小点覆盖,即最大匹配.[这个建图思想太妙了!赞!] 代码: int n,k; vector<int> graph[505]; bool bmask[505]; int cx[50