HDU_1533 Going Home (optimal matching)

Source: Internet
Author: User

HDU_1533 Going Home (optimal matching)

To be honest, at the beginning, this question does not really show that it is the maximum right match under the complete match (of course, this can also be done using network streams. (It should be to add the Source and Sink points. The distance from the source point to each m takes the smallest one from m to all H (the biggest one after the loss of a large number) and the distance from the sink point to each H is similar, and try again if you have time ). I saw it in graph theory 500 in the network flow basics. At first, I couldn't figure out how to stream it! This is the optimal matching for Binary graphs. So I spent a few hours reading the relevant information yesterday and wrote an hdu2255. It was obviously easy to write this question today. We also want to use network streams. It is found that the network stream and the binary match are still related.

  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. # Include
  6. # Include
  7. # Define maxn105
  8. Using namespace std;
  9.  
  10. Int n, m, numm, numh;
  11. Int map [MAXN] [MAXN], lx [MAXN], ly [MAXN], vx [MAXN], vy [MAXN], matchy_x [MAXN];
  12. Char s [MAXN] [MAXN];
  13. Int abs (int a) {return a <0? -A: ;}
  14.  
  15. Bool hungary (int u)
  16. {
  17. Int I;
  18. Vx [u] = 1;
  19. For (I = 0; I {
  20. If (vy [I] | map [u] [I]! = Lx [u] + ly [I]) continue;
  21. Vy [I] = 1;
  22. If (matchy_x [I] =-1 | hungary (matchy_x [I])
  23. {
  24. Matchy_x [I] = u;
  25. Return 1;
  26. }
  27. }
  28. Return 0;
  29. }
  30.  
  31. Void EK_match ()
  32. {
  33. Int I, j;
  34. For (I = 0; I {
  35. Int maxx = 0;
  36. For (j = 0; j If (map [I] [j]> maxx) maxx = map [I] [j];
  37. Lx [I] = maxx;
  38. }
  39. For (I = 0; I {
  40. While (1)
  41. {
  42. Memset (vx, 0, sizeof (vx ));
  43. Memset (vy, 0, sizeof (vy ));
  44. If (hungary (I ))
  45. Break;
  46. Else
  47. {
  48. Int temp = INT_MAX;
  49. For (j = 0; j For (int k = 0; k If (! Vy [k] & temp> lx [j] + ly [k]-map [j] [k])
  50. Temp = lx [j] + ly [k]-map [j] [k];
  51. For (j = 0; j {
  52. If (vx [j]) lx [j]-= temp;
  53. If (vy [j]) ly [j] + = temp;
  54. }
  55. }
  56. }
  57. }
  58. }
  59.  
  60. Int main ()
  61. {
  62. Int I, j;
  63. While (scanf (% d, & n, & m )! = EOF & (n | m ))
  64. {
  65. For (I = 0; I Scanf (% s, s [I]);
  66. Numm = 0;
  67. For (I = 0; I {
  68. For (j = 0; j {
  69. If (s [I] [j] = 'M ')
  70. {
  71. Numh = 0;
  72. For (int k = 0; k {
  73. For (int l = 0; l {
  74. If (s [k] [l] = 'H ')
  75. Map [numm] [numh ++] = 300-(abs (I-k) + abs (j-l ));
  76. }
  77. }
  78. Numm ++;
  79. }
  80. }
  81. }
  82. Memset (matchy_x,-1, sizeof (matchy_x ));
  83. EK_match ();
  84. Int ans = 0;
  85. For (I = 0; I Ans + = (300-map [matchy_x [I] [I]);
  86. Printf (% d, ans );
  87. }
  88. Return 0;
  89. }
    The following is an optimized slack array:

     

     

    1. # Include
    2. # Include
    3. # Include
    4. # Include
    5. # Include
    6. # Include
    7. # Define maxn105
    8. Using namespace std;
    9.  
    10. Int n, m, numm, numh;
    11. Int map [MAXN] [MAXN], lx [MAXN], ly [MAXN], vx [MAXN], vy [MAXN], matchy_x [MAXN], slack [MAXN];
    12. Char s [MAXN] [MAXN];
    13. Int abs (int a) {return a <0? -A: ;}
    14. Int min (int a, int B) {return
    15. Bool hungary (int u)
    16. {
    17. Int I;
    18. Vx [u] = 1;
    19. For (I = 0; I {
    20. If (vy [I]) continue;
    21. If (map [u] [I] = lx [u] + ly [I])
    22. {
    23. Vy [I] = 1;
    24. If (matchy_x [I] =-1 | hungary (matchy_x [I])
    25. {
    26. Matchy_x [I] = u;
    27. Return 1;
    28. }
    29. }
    30. Else slack [I] = min (slack [I], lx [u] + ly [I]-map [u] [I]);
    31. }
    32. Return 0;
    33. }
    34.  
    35. Void EK_match ()
    36. {
    37. Int I, j;
    38. For (I = 0; I {
    39. Int maxx = 0;
    40. For (j = 0; j If (map [I] [j]> maxx) maxx = map [I] [j];
    41. Lx [I] = maxx;
    42. }
    43. For (I = 0; I {
    44. Memset (slack, 127, sizeof (slack ));
    45. While (1)
    46. {
    47. Memset (vx, 0, sizeof (vx ));
    48. Memset (vy, 0, sizeof (vy ));
    49. If (hungary (I ))
    50. Break;
    51. Else
    52. {
    53. Int temp = INT_MAX;
    54. For (j = 0; j If (temp> slack [j]) temp = slack [j];
    55. For (j = 0; j {
    56. If (vx [j]) lx [j]-= temp;
    57. If (vy [j]) ly [j] + = temp;
    58. Else slack [j]-= temp;
    59. }
    60. }
    61. }
    62. }
    63. }
    64.  
    65.  
    66. Int main ()
    67. {
    68. Int I, j;
    69. While (scanf (% d, & n, & m )! = EOF & (n | m ))
    70. {
    71. For (I = 0; I Scanf (% s, s [I]);
    72. Numm = 0;
    73. For (I = 0; I {
    74. For (j = 0; j {
    75. If (s [I] [j] = 'M ')
    76. {
    77. Numh = 0;
    78. For (int k = 0; k {
    79. For (int l = 0; l {
    80. If (s [k] [l] = 'H ')
    81. Map [numm] [numh ++] = 300-(abs (I-k) + abs (j-l ));
    82. }
    83. }
    84. Numm ++;
    85. }
    86. }
    87. }
    88. Memset (matchy_x,-1, sizeof (matchy_x ));
    89. EK_match ();
    90. Int ans = 0;
    91. For (I = 0; I Ans + = (300-map [matchy_x [I] [I]);
    92. Printf (% d, ans );
    93. }
    94. Return 0;
    95. }

       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.