fork download
  1. from numpy import matrix
  2. import numpy as np
  3. n = 2
  4. k = 4
  5. A = matrix([[1,3],
  6. [2,4]
  7. ])
  8.  
  9. sc = [0]*n
  10. sr = [0]*n
  11. for i in range(0,n):
  12. sc[i] = sum( A.A[:,i] )
  13. sr[i] = sum( A.A[i,:] )
  14. output=0
  15.  
  16. def whichmin(c,r, iter):
  17. if(iter < 0):
  18. return 1
  19. r[np.argmin(r)] += n
  20. c[np.argmin(c)] += n
  21. if ( np.amin(c) < np.amin(r) ):
  22. return 1
  23. elif ( np.amin(r) < np.amin(c) ):
  24. return 0
  25. else :
  26. return whichmin(c,r, (iter -1) )
  27.  
  28. for j in range(0,k):
  29. minc = np.amin(sc)
  30. minr = np.amin(sr)
  31. if (minc < minr):
  32. incrindex = np.argmin(sc)
  33. sc[incrindex]+=n
  34. sr = [x+1 for x in sr]
  35. output += minc
  36. elif (minr < minc):
  37. incrindex = np.argmin(sr)
  38. sr[incrindex]+=n
  39. sc = [x+1 for x in sc]
  40. output += minr
  41. else :
  42. min = whichmin(sc.copy(),sr.copy(), (n - i) )
  43. if (min==1):
  44. incrindex = np.argmin(sc)
  45. sc[incrindex]+=n
  46. sr = [x+1 for x in sr]
  47. output += minc
  48. else:
  49. incrindex = np.argmin(sr)
  50. sr[incrindex]+=n
  51. sc = [x+1 for x in sc]
  52. output += minr
  53.  
  54. print(output)
  55. print(sc)
  56. print(sr)
Success #stdin #stdout 0.2s 27400KB
stdin
Standard input is empty
stdout
[3, 7]
[4, 6]
----------
[5, 7]
[5, 7]
----------
[7, 7]
[6, 8]
----------
[8, 8]
[8, 8]
----------
[10, 8]
22
[9, 9]