hdu2119Matrix (二分匹配,最小顶点覆盖)

Matrix

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1819 Accepted Submission(s): 792

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

#include<stdio.h>
#include<string.h>
int map[105][105],vist[105],match[105],m;
int find(int i)
{
    for(int j=1;j<=m;j++)
    if(!vist[j]&&map[i][j])
    {
        vist[j]=1;
        if(!match[j]||find(match[j]))
        {
            match[j]=i; return 1;
        }
    }
    return 0;
}
int main()
{
    int a,n;
    while(scanf("%d",&n)>0&&n)
    {
        scanf("%d",&m);
        memset(map,0,sizeof(map));
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a);
            if(a) map[i][j]=1;
        }
        int ans=0;
        memset(match,0,sizeof(match));
        for(int i=1;i<=n;i++)
        {
            memset(vist,0,sizeof(vist));
            ans+=find(i);
        }
        printf("%d\n",ans);
    }
}

hdu2119Matrix (二分匹配,最小顶点覆盖)

时间: 2024-07-29 05:57:26

hdu2119Matrix (二分匹配,最小顶点覆盖)的相关文章

POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以行列分别作为2个顶点集 首先以每一行来看,把这一行里面连续的*编号,作为一个顶点 再以每一列来看,把这一列里面连续的*编号,作为一个顶点 则每一个*都有2个编号,以行看时有一个,以列看时有一个,则把这2个编号连边,容量为1 再建一个源点,连接所有行的编号,一个汇点,连接所有列的编号 这道题要求的是,

POJ 1325 Machine Schedule(二分匹配 最小点覆盖)

题目链接:http://poj.org/problem?id=1325 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that m

hdu 4169 二分匹配最大独立集 ***

题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #incl

hdu1150Machine Schedule (二分匹配,最小顶点覆盖)

Problem Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the typ

hdu1281棋盘游戏(二分匹配,最小顶点覆盖)

Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的"车",并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击. 所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的"车"的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的"车

【二分匹配入门专题1】G - Asteroids poj3041【最小顶点覆盖】

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

hdu1151 poj1422 最小路径覆盖.最大二分匹配

Air RaidTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 1422 Appoint description: Description Consider a town where all the streets are one-way and each street leads from one intersection to another.

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

LA 3126 二分匹配---DAG中的最小路径应用

题意:有 n 个顾客 , 需要坐出租车从一个地方去另一个地方 , 每个顾客的出发时间.出发地点.目的地点都已给出 , 从出发地点到目的地点的时间为两地之间的路径长度 , 并且出租车要比顾客的出发时间早一分钟到达 , 问最少需要派出多少辆出租车. 解法:我们先这样来构图 , 每个顾客是一个结点,如果同一个出租车在接完客人 u 之后还来得及节客人 v , 那么就在 u 到 v 之间连一条有向边 . 由此可以发现 , 这个图是一个DAG , 那么我们就只需要找最小路径覆盖(最小路径覆盖:是指在图中找尽