zoukankan      html  css  js  c++  java
  • HDU2119--Matrix

    Matrix
    Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1309 Accepted Submission(s): 572


    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

    把行看做一个集合,把列看做一个集合

    最小顶点覆盖=最大匹配

     1 #include<cstring>
     2 #include<cmath>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstdio>
     6 using namespace std;
     7 
     8 int link[1001][1001];
     9 int cx[1001];
    10 int cy[1001];
    11 int mk[1001];
    12 
    13 void init()
    14 {
    15     memset(link,0,sizeof(link));
    16     memset(cx,0xff,sizeof(cx));
    17     memset(cy,0xff,sizeof(cy));
    18     memset(mk,0,sizeof(mk));
    19 }
    20 int nx,ny;
    21 
    22 int path(int u)
    23 {
    24     int v;
    25     for(v=1;v<=ny;v++)
    26     {
    27         if(!mk[v]&&link[u][v])
    28         {
    29             mk[v]=1;
    30             if(cy[v]==-1||path(cy[v]))
    31             {
    32                 cx[u]=v;
    33                 cy[v]=u;
    34                 return 1;
    35             }
    36         }
    37     }
    38     return 0;
    39 }
    40 
    41 int maxmatch()
    42 {
    43     int i;
    44     int sum=0;
    45     for(i=1;i<=nx;i++)
    46     {
    47         if(cx[i]==-1)
    48         {
    49             memset(mk,0,sizeof(mk));
    50             sum+=path(i);
    51         }
    52     }
    53     return sum;
    54 }
    55 
    56 int main()
    57 {
    58     while(scanf("%d",&nx)!=EOF)
    59     {
    60         if(!nx)break;
    61         init();
    62         scanf("%d",&ny);
    63         int i,j;
    64         for(i=1;i<=nx;i++)
    65         {
    66             for(j=1;j<=ny;j++)
    67             {
    68                 scanf("%d",&link[i][j]);
    69             }
    70         }
    71         printf("%d
    ",maxmatch());
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    CSS 样式书写规范
    css等比例分割父级容器(完美三等分)
    右上角标的效果
    webstorm 激活破解方法大全
    ios开关按钮
    javascript功能封装
    Ajax 跨域
    javascript常用实例的实现与封装
    使用CSS完成元素居中的七种方法
    美化加载失败的图片
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3216693.html
Copyright © 2011-2022 走看看