UV 11997 K smallest sums

Source: Internet
Author: User

Uva_11997

Because K-row processing is cumbersome, so a feasible idea is to treat as two rows, so that the processing of K-1 times can be.

When there are only two rows, you can consider how to construct the sum one by one from small to large. This can be achieved through the priority queue: first put the two smallest values in, then, the minimum and the two numbers are taken out each time, and the first number is changed to a slightly larger number in the first row, and then placed in the priority queue, then try to replace the second number with a larger number in the second row into the priority queue. In this way, the first k elements retrieved from the priority queue are the sum of the first K.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Mind 562510 # Define Minks 760 # Define INF 0x3f3f3f Int N, D, Min [ 4 * Mind], a [mink] [mink], ANS [mink];  Void  Init (){  Int  I, J;  For (I = 0 ; I <n; I ++ ){  For (J = 0 ; J <n; j ++) scanf ( "  % D " ,& A [I] [J]); STD: Sort (A [I], a [I] + N );}  For (D = 1 ; D <n * n + 2 ; D <= 1  ); Memset (Min,  0x3f , Sizeof (Min [ 0 ]) * 2 * D );} Void Update ( Int  I ){  For (; I ^ 1 ; I >>= 1  ) Min [I > 1 ] = STD: min (Min [I], Min [I ^ 1  ]);}  Int  GETID (){  Int  I;  For (I = 1 ; I < D ;){  If (Min [I < 1 ] = Min [I]) I <= 1  ;  Else I = I < 1 | 1  ;}  Return  I ;}  Void  Pop (){ Int I = GETID (); Min [I] = INF, update (I );}  Void Push ( Int X, Int Y, Int  V ){  Int I = x * n + Y; Min [d + I] = V, update (D + I );}  Void Top ( Int & X,Int & Y, Int & V ){  Int I = GETID ()- D; x = I/N, y = I % N, V = min [d + I];}  Void  Solve (){  Int  I, j, X, Y, V, CNT;  For (I = 1 ; I <n; I ++ ){  While (Min [ 1 ]! = INF) Pop (); CNT = 0  ; Push (  0 , 0 , A [I- 1 ] [ 0 ] + A [I] [ 0  ]);  While (CNT < N) {top (X, Y, v), pop (); ans [CNT ++] = V; If (X <n) Push (x + 1 , Y, a [I- 1 ] [X + 1 ] + A [I] [Y]);  If (Y <n) Push (X, Y + 1 , A [I- 1 ] [X] + A [I] [Y + 1  ]);}  For (J = 0 ; J <n; j ++) A [I] [J] = Ans [J];} printf ( "  % D  " , A [n- 1 ] [ 0  ]);  For (I = 1 ; I <n; I ++) printf ( "  % D  " , A [n- 1  ] [I]); printf (  "  \ N  " );}  Int  Main (){  While (Scanf ( "  % D  " , & N) = 1  ) {Init (); solve ();}  Return   0  ;} 

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.