题意:

N*N网格中有小行星,光束能将一整行或者一整列的行星消灭,问消灭所有行星至少需要多少光束?

分析:

最小顶点覆盖问题,将每个小行星看成边,左右顶点为横纵坐标,可以转化为二分图,利用二分图中最小顶点覆盖等于最大二分匹配的性质,求出最大二分匹配(匈牙利算法)即可。

代码:

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10005;
int x[maxn], y[maxn];
int used[maxn], match[maxn];
int N, K;
vector<int>G[maxn];
void add_edge(int u, int v)
{
G[v].push_back(u);
G[u].push_back(v);
}
int dfs(int v)
{
used[v] = 1;
for(int i = 0; i < G[v].size(); i++){
int u = G[v][i], w = match[u];
if(w<0||!used[w]&&dfs(w)){
match[v] = u;
match[u] = v;
return 1;
}
}
return 0;
}
int bipartite_matching()
{
int res = 0;
memset(match, -1, sizeof(match));
for(int i = 1; i <= 2 * N; i++){
if(match[i]<0){
memset(used, 0, sizeof(used));
if(dfs(i))
res++;
}
}
return res;
}
int main (void)
{
scanf("%d%d",&N,&K);
memset(match, -1, sizeof(match));
for(int i = 0; i < K; i++){
scanf("%d%d",&x[i],&y[i]);
add_edge(x[i], y[i] + N);
}
printf("%d\n", bipartite_matching());
}

POJ 3041_Asteroids的更多相关文章

  1. POJ训练计划3041_Asteroids(二分图/最小点覆盖=最大匹配)

    解题报告 http://blog.csdn.net/juncoder/article/details/38135053 题目传送门 题意: 给出NxN的矩阵,有M个点是障碍 每次仅仅能删除一行或者一列 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. composer Failed to decode zlib stream 无法解码zlib流

    Win7 中安装 Composer (PHP) 国内有些网络不能访问美国的Composer官网,可访问 Composer 中文网 学习. 目标 可以在任何目录下的项目中执行 PHP composer. ...

  2. WinForm 对话框,流

    private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...

  3. SCANF输入错误

    while((a<=0||a>=10)||(b<=0||b>=10))    {        fflush(stdin);        cout<<" ...

  4. 数据库时间类型是 datetime 类型的处理

    当数据库时间类型是datetime类型时,获取前台时间是要这样处理 ,因为数据库是把时间转换为字符类型来比较的 sb.Append(" and Date <=convert(varch ...

  5. IPython notebook快捷键(Jupyter notebook)

    转自“https://blog.csdn.net/eswai/article/details/53642802” 本文整理了神器IPython Notebook(或Jupyter Notebook)的 ...

  6. spark集群启动步骤及web ui查看

    集群启动步骤:先启动HDFS系统,在启动spark集群,最后提交jar到spark集群执行. 1.hadoop启动cd /home/***/hadoop-2.7.4/sbinstart-all.sh ...

  7. 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(三)-----mapGetters

    简单的理解: const getters = { newCount: function(state){ return state.count += 5; } } ------------------- ...

  8. ZooKeeper系列(四)

    一.配置服务 配置服务是分布式应用所需要的基本服务之一,它使集群中的机器可以共享配置信息中那些公共的部分.简单地说,ZooKeeper可以作为一个具有高可用性的配置存储器,允许分布式应用的参与者检索和 ...

  9. jekyll 将纯文本转化为静态网站和博客 静态网站生成器

    jekyll 将纯文本转化为静态网站和博客 静态网站生成器 这个貌似对windows 支持不是很好~ 但是有支持,官方说不建议使用

  10. 1434:【例题2】Best Cow Fences

    1434:[例题2]Best Cow Fences 时间限制: 1000 ms         内存限制: 65536 KB提交数: 263     通过数: 146 [题目描述] 给定一个长度为n的 ...