Matrix

            Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                Total Submission(s): 3095    Accepted Submission(s): 1428

Problem Description
Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column .

Your task is to give out the minimum times of deleting all the '1' in the matrix.

 
Input
There are several test cases.

The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.

n=0 indicate the end of input.

 
Output
For each of the test cases, in the order given in the input, print one line containing the minimum times of deleting all the '1' in the matrix.
 
Sample Input
3 3 0 0 0 1 0 1 0 1 0 0
 
Sample Output
2
 
Author
Wendell
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2115 2117 2114 2118 2120 

     

题目大意:

给你一个矩阵(只包含0或1),每次你可以选择一行或一列,并删除这一行或这个列中的所有“1”。您的任务是给出删除矩阵中所有“1”的最小时间。

思路:

这个题跟我们前面做的那个碰气球的题一样,是哪个题中的一小部分。

我们要将所有的1都删去,这就跟我们那个题中的我们选定一种颜色,将其全部消灭用的操作数。同样我们每一次只能对一行或者一列进行操作。这样我们对于1的位置的行与列连边,再把这两个(列和边)看作是两个集合。求它的最大匹配数。

一个很简单的二分图的板子。。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1500+10
using namespace std;
bool vis[N];
int n,m,k,x,y,girl[N],map[N][N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int find(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=true;
            if(girl[i]==-1||find(girl[i])) {girl[i]=x;return 1;}
        }
    }
    return 0;
}
int main()
{
    int t=0,sum,ans;
    while(1)
    {
        n=read();if(n==0) break;
        m=read(),ans=0;
        memset(map,0,sizeof(map));
        for(int i=1;i<=n;i++)
         for(int j=1;j<=m;j++)
         {
             x=read();
            if(x==1) map[i][j]=1;
          }
        memset(girl,-1,sizeof(girl));
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

HDU——2119 Matrix的更多相关文章

  1. hdu 2119 Matrix(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others)    ...

  2. HDU 2119 Matrix

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2119 解题思路: 处理数据,使用公式最小点覆盖数=最大匹配数,使用匈牙利算法求二分图最大匹配即可. ...

  3. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

  4. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  5. HDU——T 2119 Matrix

    http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  6. hdu 2119(简单二分图) Matrix

    http://acm.hdu.edu.cn/showproblem.php?pid=2119 一个由0和1构成的矩阵,每次选取一行或者一列将其中的1变成0,求最小删除次数 简单的二分图应用,矩阵的横坐 ...

  7. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  8. hdu 5569 matrix dp

    matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5569 D ...

  9. HDU 5671 Matrix 水题

    Matrix 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5671 Description There is a matrix M that has ...

随机推荐

  1. Java练习题00

    问题: 将一个数字与数组中的元素比较, 如果该数字存在数组中,给出该数字在数组中的位置: 如果该数字不在数组中,给出提示.   代码: public class Page84{     public ...

  2. Java递归调用改成非递归

          在java语言中,使用递归调用时,如果过多的调用容易造成java.lang.StackOverflowError即栈溢出和程序执行过慢.这是一个潜在Bug和影响程序执行效率问题,需要谨慎使 ...

  3. 使用Eclipse进行PHP的服务器端调试

    最近工作需要对PHP的服务器端代码进行远程调试,涉及到Eclipse里环境的设置.在网上找了很多资料,大多不全,或者缺少配图,于是把自己做的过程中遇到的问题记录了下来,希望对需要的朋友们有所帮助. 首 ...

  4. Python3简明教程(十)—— 异常

    在本节我们学习 Python 的异常以及如何在你的代码中处理它们. 异常 在程序执行过程中发生的任何错误都是异常.每个异常显示一些相关的错误信息,比如你在 Python3 中使用 Python2 独有 ...

  5. centOS7安装 mysql-community-release-el7-5.noarch.rpm 包

    一.rpm包 1.wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm(下载rpm) 2.rpm -ivh mysql ...

  6. SQL分组聚合查询练习(SQL Server和Oracle相似)20190514

    先建表 CREATE TABLE [dbo].[orderdt_jimmy]( ,) NOT NULL, [order_nid] [int] NOT NULL, ) NOT NULL, [qty] [ ...

  7. Hydraulic Motor Manufacturers - What Is A Cycloidal Hydraulic Motor?

    The     Hydraulic Motor manufacturers    stated that the cycloidal hydraulic motor is fixedly connec ...

  8. JavaSE-29 Java8的Lambda表达式

    概念说明 Lambda表达式是Java8提供的新特性,支持将代码块作为方法的参数. Lambda表达式支持使用简洁的代码创建只有一个方法的接口(函数式接口). 只包含一个方法的接口也称为函数式接口. ...

  9. Android UI: LinearLayout中layout_weight 属性的使用规则

    首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...

  10. Brackets POJ - 2955

    解法 区间dp例题,每次枚举分段点的时候先更新如果开始到结束区间端点有闭合的括号,那么dp[start][end]=dp[start+1][end-1]+2其他照常枚举即可 代码 #include & ...