HDOJ Matrix

Matrix

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

Total Submission(s): 2112    Accepted Submission(s): 932

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

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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int map[110][110],vis[110];
int N,M,point[110];
bool DFS(int a){
    for(int i=0;i<M;++i){
        if(!vis[i]&&map[a][i]){
            vis[i]=1;
            if(point[i]<0||DFS(point[i])){
                point[i]=a;
                return true;
            }
        }
    }
    return false ;
}
int count(){
    int ans=0;
    for(int i=0;i<N;++i){
        memset(vis,0,sizeof(vis));
        if(DFS(i)){
            ans++;
        }
    }
    return ans;
}
int main()
{
    int i,j;
    while(scanf("%d",&N),N){
        cin>>M;
        memset(point,-1,sizeof(point));
        memset(map,0,sizeof(map));
        for(i=0;i<N;++i){
            for(j=0;j<M;++j){
                cin>>map[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}
时间: 2024-08-09 21:59:59

HDOJ Matrix的相关文章

HDOJ 3376 Matrix Again

和HDOJ 2686 一样,只是范围不同 最大费用最大流..... 与最小费用最大流的区别用////////////标出来了 对于detour,在源点和汇点处的边的流量为2 对于每个点只能经过一次,拆点,两个点直接建一条流量为1,费用为mp[i][j]的边 对于每个点可以走到他的左边和下边:连一个费用为0流量大于1的边 Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Jav

HDOJ 4965 Fast Matrix Calculation

(AB)^n=A*(BA)^(n-1)^B Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 576    Accepted Submission(s): 297 Problem Description One day, Alice and Bob felt bored again, B

hdoj 3376,2686 Matrix Again 【最小费用最大流】

题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次只能向右或者向下,出了末尾点其他只能走一次,不能交叉,每次走到一个格子拿走这个格子中的数字,求价值最大? 分析:很明显费用流,开始想的到一种建图方案,但是那样的话流量全为负值的话会成一个环,所以果断换了. 建图方案是: 首先拆点,每个点拆成两个i 和 ii ,建边,费用为当前格子的值,流量为1,初始点和末尾点为2 然后每个点向它的右边和下边分别建边,容量为1,费用为0 s 连接 左上角

HDOJ 3666 THE MATRIX PROBLEM 差分约束

根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系..... THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 1914 Problem Description You have been given a matrix CN

hdoj 3376 Matrix Again and hdoj 2686 Matrix 【最大费用最大流】

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 3453    Accepted Submission(s): 1017 Problem Description Starvae very like play a number game in the n*n Matrix. A positive intege

HDOJ 233 Matrix 5015【矩阵快速幂】

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1355    Accepted Submission(s): 806 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may

HDOJ 题目4313 Matrix(类似克鲁斯卡尔)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 977 Problem Description Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N

HDOJ 5015 233 Matrix

构造矩阵+快速幂 233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 344    Accepted Submission(s): 231 Problem Description In our daily life we often use 233 to express our feelings. Actually,

HDOJ 5671 Matrix

Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 780    Accepted Submission(s): 330 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we per