hdu Kaka's Matrix Travels (minimum cost maximum flow)

Write about the meaning of the questions:   to give you a n * n matrix, each grid has a non-negative integer, he went to the lower right corner from the top left corner, collect digital walked, cumulative, but only to the right or go down and then walked digital becomes 0, so that you seek from upper left to lower right corner, and then go k times, the maximum income is.

The typical minimum cost maximum flow entry problems, only in the construction of FIG., The source provided to the cap 1 to k, when the finish time of the process becomes 0, just point-split, split into two sides, one 1 capacity, the right value of the right point and the other is a springboard, INF capacity, the cost is 0, can be

****************************************************************************************************************

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #define M 200001
  4 #define maxx 2000
  5 #define Max(a, b) a > b ? a : b
  6 #define Min(a, b) a < b ? a : b
  7 #define inf (1 << 29)
  8 struct T
  9 {
 10     int u, v, val, next, cost;
 11 } e[M];
 12 int th;
 13 int visit[M], pre[M], dis[M], que[M], g[M], pos[M], map[100][100];
 14 void add(int u, int v, int val, int cost)
 15 {
 16     e[th].u = u, e[th].v = v, e[th].val = val, e[th].cost = cost;
 17     e[th].next = g[u], g[u] = th++;
 18     e[th].u = v, e[th].v = u, e[th].val = 0, e[th].cost = -cost;
 19     e[th].next = g[v], g[v] = th++;
 20 }
 21 int spfa(int s, int t, int n)
 22 {
 23     intI, U, V, K;
 24      for (I = 0 ; I <= n-; I ++ )
 25      {
 26 is          pre [I] = - . 1 , Visit [I] = 0 ;
 27      }
 28      int head, tail;
 29      head = tail = 0 ;
 30      for (I = 0 ; I <= n-; I ++) DIS [I] = - . 1 ; // when seeking minimum costs here have to be positive infinity 
31 is      que [tail ++] = S, pre [S ] = S, DIS [S] = 0 , Visit [S] = . 1 ;
 32      the while (head =! tail)
 33 is     {
 34 is          int U = que [head ++ ];
 35          Visit [U] = 0 ;
 36          for (K = G [U]; K = -! . 1 ; K = E [K] .next)
 37 [          {
 38 is              V = E [ K] .v;
 39              IF (E [K] .val> 0 && DIS [U] + E [K] .cost> DIS [V]) // maximum cost, the minimum cost required to change the notation 
40              {
 41                  DIS [V] = DIS [U] + E [K] .cost;
 42 is                  pre [V] = U;
 43 is                  POS [V] = K;
 44 is                 IF (! Visit [V])
 45                  {
 46 is                      Visit [V] = . 1 ;
 47                      que [tail ++] = V;
 48                  }
 49              }
 50          }
 51 is      }
 52 is      IF ! (pre [T] = - . 1 && DIS [T] > - . 1 ) // when seeking minimum costs changed here dit [t] <+ infinity 
53 is      {
 54 is          return  . 1 ;
 55      }
 56 is      return  0 ;
 57 is  }
 58 int MinCostFlow ( int s, int t, int n-)
 59  {
 60      IF (s == t)
 61 is      {
 62 is          // herein specific conditions, if there is additional with t s do not, if the point data is used as s with t have to consider
 63          // directly return a value. Otherwise spfa in the queue will be an endless loop. 
64      }
 65      int Flow = 0 , cost = 0 ;
 66      the while (SPFA (S, T, n-))
 67      {
 68          int U, min = INF;
 69          for (U = T;! U = S; U = pre [ U])
 70         {
 71             min = Min(min, e[pos[u]].val);
 72         }
 73         flow += min;
 74         cost += dis[t] * min;
 75         for (u = t; u != s; u = pre[u])
 76         {
 77             e[pos[u]].val -= min;
 78             e[pos[u]^1].val += min;
 79         }
 80     }
 81     return cost;
 82 }
 83 int main()
 84 {
 85     int n, m, i, j, k, s, t, u, v, cost, ans, x, num, tt;
 86     while (scanf("%d%d", &n, &m) - EOF)
 87     {
 88         for (i = 0, th = 0; i < n * n * 3 + 2; i++)
 89         {
 90             g[i] = -1;
 91         }
 92         for (i = 0; i < n; i++)
 93         {
 94             for (j = 0; j < n; j++)
 95             {
 96                 scanf("%d", &map[i][j]);
 97             }
 98         }
 99         num = n * n;
100         for (i = 0; i < n; i++)
101         {
102             for (j = 0; j < n; j++)
103             {
104                 x = i * n + j;
105                 add(x, x + num, 1, map[i][j]);
106                 add(x, x + num, inf, 0);
107                 if (i + 1 < n)
108                 {
109                     add(x + num, x + n, inf, 0);
110                 }
111                 if (j + 1 < n)
112                 {
113                     add(x + num, x + 1, inf, 0);
114                 }
115             }
116         }
117         s = 2 * n * n, t = s + 1, n = t + 1;
118         add(s, 0, m, 0);
119         add(s - 1, t, m, 0);
120         ans = MinCostFlow(s, t, t + 1);
121         printf("%d\n", ans);
122     }
123     return 0;
124 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3534734.html

Guess you like

Origin blog.csdn.net/weixin_34166472/article/details/93432954