We are enjoying the pleasure of completely conquering a question...
It is tortuous to do this. The first thing I want to do right away is to use the K-times DP. I quickly knocked it out, and then decided to WA. I couldn't figure out how to generate data by myself. I found that DP is difficult to solve this problem, because K-times of DP is equivalent to greedy in this question, this question is obviously greedy.
Look at the data and you will know:
4 3
1 2 3 5
0 2 1 1
1 4 2 3
3 4 1 2
It is found that if we find the greatest path each time, the final result is 32 and the obvious result is 34. This is the best step, but the final result is not guaranteed to be the best.
At this time, it is found that the backflow of the network stream can solve this problem very well, because there is reflux, so it can ensure that the minimum cost of K streams. Because the Network FlowAlgorithmIn the subsequent process of searching for augmented routes, reflux can change the flow direction of the previous feasible streams (I envy the nature of the network stream, but I can make up for it later if I am wrong, in this way, the result is the best, but only one DFS can be thought of in life. You can only go down and cannot go back ).
Why is the minimum cost required? Isn't the maximum value required in the question ?, This problem is well solved. You can use the maximum path algorithm when looking for a feasible stream, or change the cost of each vertex to a negative value.
Creating a graph is simple. Because each vertex can only be used once in the question, it is necessary to split the vertex.
KaKa's matrix travels
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:6277 |
|
Accepted:2483 |
Description OnN×NChessboard with a non-negative number in each grid, Kaka starts his matrix travelsSum= 0. for each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. kaKa adds the numberSumIn each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSumKaKa can obtain for his first travel. Now Kaka is wondering what is the maximumSumHe can obtain after hisKTh travel. NoteSumIs accumulative duringKTravels. Input The first line contains two integersNAndK(1 ≤N≤ 50, 0 ≤K≤ 10) described above. The followingNLines represents the Matrix. You can assume the numbers in the matrix are no more than 1000. Output The maximumSumKaKa can obtain after hisKTh travel. Sample Input 3 21 2 30 2 11 4 2 Sample output 15 Source Poj monthly -- 2007.10.06, Huang, Jinsong |
# Include <stdio. h> # Include < String . H># Include <Algorithm> # Include <Iostream> Using Namespace STD; # Define N 5050 # Define INF 0x3fffffff Struct Node { Int To, next, W, C;} edge [n * N]; Int N, K; Int S, T; Int CNT, pre [N]; Int Que [N * 100 ]; Int Point [N], pedge [N]; Void Add_edge ( Int U, Int V, Int W, Int C) {edge [CNT]. = V; edge [CNT]. W = W; edge [CNT]. c = C; edge [CNT]. Next = Pre [u]; Pre [u] = CNT ++ ;} Int Spfa (){ Int QF = 1 , Qd = 0 ; Int Dis [N]; Int Mark [N]; memset (point, -1 , Sizeof (Point); memset (pedge, - 1 , Sizeof (Pedge )); For ( Int I = 0 ; I <= T; I ++ ) {Dis [I] = INF; Mark [I] = 0 ;} Que [ 0 ] =S; Mark [s] = 1 ; DIS [s] = 0 ; While (QF> QD ){ Int Cur = que [QD ++ ]; Mark [cur] = 0 ; For ( Int P = pre [cur]; P! =- 1 ; P =Edge [p]. Next ){ Int V = Edge [p].; Int W = Edge [p]. W; Int C = Edge [p]. C; If (W = 0 ) Continue ; If (DIS [v]> dis [cur] + C) {dis [v] = Dis [cur] +C; point [v] = Cur; pedge [v] = P; If (MARK [v] = 0 ) {Mark [v] = 1 ; Que [QF ++] = V ;}}}} If (DIS [T] = inf) Return 0 ; Else Return 1 ;} Int FUC (){ Int Sum = 0 ; Int TMP = T; While (TMP! = 0 ){ Int P = Pedge [TMP]; sum + = Edge [p]. C; edge [p]. W -= 1 ; Edge [p ^ 1 ]. W + = 1 ; TMP = Point [TMP];} Return SUM ;} Int Main (){ While (Scanf ( " % D " , & N, & K )! = EOF) {CNT = 0 ; Memset (PRE, - 1 , Sizeof (Pre )); For ( Int I = 1 ; I <= N; I ++ ) For ( Int J = 1 ; J <= N; j ++){ Int Id = (I- 1 ) * N + J; Int TMP; scanf ( " % D " ,& TMP); add_edge (ID, n * N + id, 1 ,- TMP); add_edge (n * N + id, ID, 0 , TMP); add_edge (ID, n * N + id, K- 1 , 0 ); Add_edge (n * N + id, ID, 0 , 0 ); If (I! = N) {add_edge (n * N + id, ID + N, K, 0 ); Add_edge (ID + N, N * n + id, 0 ,0 );} If (J! = N) {add_edge (n * N + id, ID + 1 , K, 0 ); Add_edge (ID + 1 , N * n + id, 0 , 0 );} S = 0 ; T = N * n *2 + 1 ; Add_edge (S, 1 , K, 0 ); Add_edge ( 1 , S, 0 , 0 ); Add_edge (n * N * 2 , T, K, 0 ); Add_edge (t, 2 * N, 0 , 0 ); ///////// // Graph creation completed Int Sum = 0 ; While (Spfa () {sum + = FUC ();} printf ( " % D \ n " ,- Sum );} Return 0 ;}