hdu2119 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

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

最小顶点覆盖=最大匹配

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 #include

2 #include

3 #include

4 #include

5 #include

6 using namespacestd;7

8 int link[1001][1001];9 int cx[1001];10 int cy[1001];11 int mk[1001];12

13 voidinit()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 intnx,ny;21

22 int path(intu)23 {24 intv;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 intmaxmatch()42 {43 inti;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 returnsum;54 }55

56 intmain()57 {58 while(scanf("%d",&nx)!=EOF)59 {60 if(!nx)break;61 init();62 scanf("%d",&ny);63 inti,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\n",maxmatch());72 }73 return 0;74 }

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值