The original question is described as follows:
Time Limit: 256 ms single point time limit: Ms memory limit: MB
Description
Give you A matrix A (0 <= aij <= 100) of m x n (1 <= m, n <= 10000). You must select A number in the matrix, each row and column must have at least one number, so that the sum of the selected numbers is as small as possible.
Input
Multiple groups of test data. The first is the number of data groups T
For each group of test data, the first row is two positive integers m, n, indicating the number of rows and columns of the matrix respectively.
In the next m row, n integers are separated by A space to represent the elements of matrix.
Output
Output a row of data in each group, indicating the minimum value of the sum of the selected numbers.
Data range
Small Data: 1 <= m, n <= 5
Big Data: 1 <= m, n <= 100
-
Sample Input
-
23 31 2 33 1 22 3 15 51 2 3 4 55 1 2 3 44 5 1 2 33 4 5 1 22 3 4 5 1
-
Sample output
-
Case 1: 3Case 2: 5
I don't know why. The code I wrote is correct using multiple test examples, but it is WA (wrong answer) after uploading. The Code is as follows:
# Include <iostream>
# Include <stdlib. h>
Using namespace std;
Int getnumber (int arr [100] [100], int m, int n)
{
Int I, j, k;
Int minresult = 1000001;
Int start = 0;
If (m> = n)
{
For (k = 0; k <m; k ++)
{
Int B [100] = {0 };
Int minnum = 1000001;
Int result = 0;
Int select = 0;
For (j = 0; j <n; j ++)
{
If (B [j] = 0)
{
If (arr [start] [j] <minnum)
{
Select = j;
Minnum = arr [start] [j];
}
}
}
B [select] = 1;
Result + = arr [start] [select];
For (I = 0; I <m; I ++)
{
If (I = start) continue;
Select =-1;
Int minnum = 1000001;
For (j = 0; j <n; j ++)
{
If (B [j] = 0)
{
If (arr [I] [j] <minnum)
{
Select = j;
Minnum = arr [I] [j];
}
}
}
If (select =-1)
{
For (j = 0; j <n; j ++)
{
If (arr [I] [j] <minnum)
{
Select = j;
Minnum = arr [I] [j];
}
}
}
B [select] = 1;
Result + = arr [I] [select];
}
Minresult = result <minresult? Result: minresult;
Start ++;
}
}
Else
{
For (k = 0; k <n; k ++)
{
Int B [100] = {0 };
Int minnum = 1000001;
Int result = 0;
Int select = 0;
For (j = 0; j <m; j ++)
{
If (B [j] = 0)
{
If (arr [j] [start] <minnum)
{
Select = j;
Minnum = arr [j] [start];
}
}
}
B [select] = 1;
Result + = minnum;
For (I = 0; I <n; I ++)
{
If (I = start) continue;
Select =-1;
Int minnum = 1000001;
For (j = 0; j <m; j ++)
{
If (B [j] = 0)
{
If (arr [j] [I] <minnum)
{
Select = j;
Minnum = arr [j] [I];
}
}
}
If (select =-1)
{
For (j = 0; j <m; j ++)
{
If (arr [j] [I] <minnum)
{
Select = j;
Minnum = arr [j] [I];
}
}
}
B [select] = 1;
Result + = minnum;
}
Minresult = result <minresult? Result: minresult;
Start ++;
}
}
Return minresult;
}
Int main ()
{
Int arrnum = 0;
Cin> arrnum;
Int * result = new int [arrnum];
Int I, j, k;
For (k = 0; k <arrnum; k ++)
{
Int arr [100] [100];
Int m = 0, n = 0;
Cin> m> n;
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
{
Cin> arr [I] [j];
}
}
Result [k] = getnumber (arr, m, n );
}
For (k = 0; k <arrnum; k ++)
{
Cout <"Case" <k + 1 <":" <result [k] <endl;
}
Delete [] result;
Return 0;
}