• 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
  • 相关阅读:
    关于silverlight打印模糊的问题
    Microsoft Office Excel 不能访问文件及COM无法访问
    IE8 下 select option 内容过长 , 展开时信息显示不全解决办法
    如何用css做一个细虚线边框表格
    DIV 垂直 垂直水平 居中
    Ul li 横排 菜单
    对原生js的一些小尝试
    Nodejs学习笔记——Assert(断言)
    Octopress创建GitHub Pages——基于代码托管的静态博客
    JS倒计时器一只,顺便复习javascript时间相关函数
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3216693.html
Copyright © 2020-2023  润新知