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. jQuery ajax参数后台获取不到的问题

    <script type="text/javascript"> init(); var alldate = {a : "0",b:"1&q ...

  2. monkeyrunner 简单用例编写

    monkeyrunnerfrom com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImagedevice = Monke ...

  3. 登录脚本重构Element

    登录脚本重构Element package com.gubai.selenium; import org.openqa.selenium.By; import org.openqa.selenium. ...

  4. 李开复:AlphaGo 若打败了世界冠军,意味着什么?

    创新工场董事长李开复在知乎就AlphaGo与李世石的人机大战发表了自己看法,他认为四个月前的AlphaGo击败李世石基本不可能,不过这四个月AlphaGo进步很多,比赛应该很精彩.但是,无论这次结果如 ...

  5. ecpg - 嵌入的 SQL C 预处理器

    SYNOPSIS ecpg [ option...] file... DESCRIPTION 描述 ecpg 是一个嵌入的用于C 语言的 SQL 预编译器. 它把嵌有 SQL 语句的 C 程序通过将 ...

  6. EEPROM的存储大小

    学习单片机时,常见的EEPROM如24C02的大小为2Kbit(有的也称2KB).这里的2KB到底能存储多少数据呢? 2KB中,B表示单位bit,K表示1024. 单片机编程中常用的数据类型为unsi ...

  7. gitee 如何创建仓库 及发布

    gitee 如何创建仓库 及发布 http://pengchenggang.gitee.io/layuisyshelp ----------- 在本地项目文件中使用bash $ git config ...

  8. CPP-STL:随机数发生器random_shuffle

    //--------------------------------------------------------------------------- #include <string.h& ...

  9. Codeforces Round #555 (Div. 3) 解题报告

    A.Reachable Numbers 题意: 给定操作f(x):将x加1,删去得到的数的所有末尾0,如f(10099)=10099+1=10100→1010→101.现在给定一个数n,对n进行无限次 ...

  10. 理解 Word2Vec 之 Skip-Gram 模型

    理解 Word2Vec 之 Skip-Gram 模型 天雨粟 模型师傅 / 果粉 https://zhuanlan.zhihu.com/p/27234078 508 人赞同了该文章 注明:我发现知乎有 ...