Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16379   Accepted: 8930

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 grid.

Fortunately, Bessie has a powerful weapon that can vaporize all the
asteroids in any given row or column of the grid with a single shot.This
weapon is quite expensive, so she wishes to use it sparingly.Given the
location of all the asteroids in the field, find the minimum number of
shots Bessie needs to fire to eliminate all of the asteroids.

Input

* Line 1: Two integers N and K, separated by a single space.

* Lines 2..K+1: Each line contains two space-separated integers R
and C (1 <= R, C <= N) denoting the row and column coordinates of
an asteroid, respectively.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

  1. 3 4
  2. 1 1
  3. 1 3
  4. 2 2
  5. 3 2

Sample Output

  1. 2

Hint

INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:

X.X

.X.

.X.

OUTPUT DETAILS:

Bessie may fire across row 1 to destroy the asteroids at (1,1) and
(1,3), and then she may fire down column 2 to destroy the asteroids at
(2,2) and (3,2).

 
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <stack>
  9. #include <queue>
  10. #include <algorithm>
  11. #define N 500+20
  12. #define INF 0x3f3f3f3f
  13.  
  14. using namespace std;
  15. int n, m;
  16. int map[N][N];
  17. int link[N];
  18. bool vis[N];
  19.  
  20. bool match( int u )
  21. {
  22. for(int i=1; i<=n; i++)
  23. {
  24. if(vis[i]==false && map[u][i]==1 )
  25. {
  26. vis[i]=true;
  27. if(link[i]==-1 || match(link[i]) )
  28. {
  29. link[i]=u;
  30. return true;
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36.  
  37. int main()
  38. {
  39. int i, j, k;
  40. int u, v;
  41. scanf("%d %d", &n, &m);
  42. memset(map, 0, sizeof(map));
  43.  
  44. for(i=0; i<m; i++ ){
  45. scanf("%d %d", &u, &v);
  46. map[u][v] = 1;
  47. }
  48. memset(link, -1, sizeof(link));
  49. int ans=0;
  50.  
  51. for(int i=1; i<=n; i++)
  52. {
  53. memset(vis, false, sizeof(vis));
  54. if(match(i))
  55. ans++;
  56.  
  57. }
  58. printf("%d\n", ans );
  59. return 0;
  60. }

poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)的更多相关文章

  1. POJ 3041 Asteroids (二分图最小点覆盖)

    题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...

  2. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  3. POJ 3041 Asteroids 二分图匹配

    以行列为点建图,每个点(x,y) 对应一条边连接x,y.二分图的最小点覆盖=最大匹配 //#pragma comment(linker, "/STACK:1024000000,1024000 ...

  4. POJ 3041 Asteroids 二分图

    原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  6. POJ 3041 Asteroids 二分图之最大匹配

    题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...

  7. 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题

    学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...

  8. POJ 3041 Asteroids(二分图模板题)

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  9. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

随机推荐

  1. 最短路算法之 Dijkstra算法

    Dijkstra算法 Dijkstra算法是典型最短路算法,用于计算一个节点到其它全部节点的最短路径. 主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最短路径的最 ...

  2. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  3. 谷歌浏览器插件-html页面js事件查看器

    谷歌浏览器插件-html页面js事件查看器 1.下载 下载地址:http://files.cnblogs.com/files/graceup/VisualEvent.zip 解压得到文件:Visual ...

  4. 将VS2010里的红色波浪线去掉

    有时候,程序编译没错,却出现了一堆的红色波浪形,看着就烦. 解决方案:在VAssistX菜单栏->Visual Assist X Options->展开Advanced->Under ...

  5. hdu2141Can you find it?

     给你四个集合.要你从这四个集合中 各取出一个数出来,推断,取出的前三个数的和 是否等于第四个数. 数据比較大.我的做法是将 前两个集合全部数全部和的情况取出来, 然后二分查找第四个集合和第三集合 ...

  6. C# DataTable 总结

    (1)构造函数 DataTable()   不带参数初始化DataTable 类的新实例. DataTable(string tableName)  用指定的表名初始化DataTable 类的新实例. ...

  7. Mongo JavaTest

    import com.mongodb.MongoClient; import com.mongodb.DB; import com.mongodb.DBCollection; import com.m ...

  8. Linux 下wifi 驱动开发(三)—— SDIO接口WiFi驱动浅析

    SDIO-Wifi模块是基于SDIO接口的符合wifi无线网络标准的嵌入式模块,内置无线网络协议IEEE802.11协议栈以及TCP/IP协议栈.可以实现用户主平台数据通过SDIO口到无线网络之间的转 ...

  9. Source-php-request-2

    php比較坑的地方就是实现相同的目的,能够使用超级多种手段.比方(file_get_contents和fopen以及如今提到的curl以及fsockopen当然还有socket)这对于一个经验少的程序 ...

  10. python tkinter module的用法

    tkinter windows下从python3.2版本之后是自动安装的. python3.3之后的引入方式: >>> import _tkinter>>> imp ...