HDU 4862 Jump minimum k path overwrite fee stream, hdu4862
Gg...
Question:
Matrix given n * m
Select <= k starting points. Each starting point can jump to the right or down. The cost is two points. The Manhattan distance. If the number of the two grids is the same, the value of the number on the grid will be earned.
Q: The minimum cost of traversing the entire Graph
If the traversal fails, the output is-1.
# Include <stdio. h> # include <string. h> # include <iostream> # include <math. h> # include <queue> # include <set> # include <algorithm> # include <stdlib. h> using namespace std; # define ll int # define N 220 # define M 12345 # define inf (1 <29) // note that the vertex mark must be a [0-vertex] // bidirectional Edge. Note the REstruct Edge {ll from, to, flow, cap, nex, cost ;} edge [M * 2]; ll head [N], edgenum; void add (ll u, ll v, ll cap, ll cost) {// apply reverse arc Edge E = {u, v, 0, cap, hea to network streams D [u], cost}; edge [edgenum] = E; head [u] = edgenum ++; Edge E2 = {v, u, 0, 0, head [v],-cost}; // if the cap is a unidirectional edge, it must be 0 edge [edgenum] = E2; head [v] = edgenum ++ ;} ll D [N], P [N], A [N]; bool inq [N]; bool BellmanFord (ll s, ll t, ll & flow, ll & cost) {for (ll I = 0; I <= t; I ++) D [I] = inf; memset (inq, 0, sizeof (inq )); D [s] = 0; inq [s] = 1; P [s] = 0; A [s] = inf; queue <ll> Q; Q. push (s); while (! Q. empty () {ll u = Q. front (); Q. pop (); inq [u] = 0; for (ll I = head [u]; I! =-1; I = edge [I]. nex) {Edge & E = edge [I]; if (E. cap> E. flow & D [E. to]> D [u] + E. cost) {D [E. to] = D [u] + E. cost; P [E. to] = I; A [E. to] = min (A [u], E. cap-E. flow); if (! Inq [E. to]) Q. push (E. to), inq [E. to] = 1 ;}}} if (D [t] = inf) return false; flow + = A [t]; cost + = D [t] * A [t]; ll u = t; while (u! = S) {edge [P [u]. flow + = A [t]; edge [P [u] ^ 1]. flow-= A [t]; u = edge [P [u]. from;} return true;} ll flow; ll Mincost (ll s, ll t) {// return minimum cost flow = 0; ll cost = 0; while (BellmanFord (s, t, flow, cost); return cost;} void init () {memset (head,-1, sizeof head); edgenum = 0;} ll n, m, k; ll Hash1 (ll x, ll y) {return (x-1) * m + y;} ll Hash2 (ll x, ll y) {return n * m + (x-1) * m + y;} char s [20]; ll mp [20] [20]; int main () {int T, I, j, g, Cas = 1; scanf ("% d", & T); while (T --) {cin> n> m> k; for (I = 1; I <= n; I ++) {scanf ("% s", s + 1); for (j = 1; j <= m; j ++) mp [I] [j] = s [j]-'0';} printf ("Case % d:", Cas ++); init (); ll I, j, g; ll from = 0, to = Hash2 (n, m) + 10, jiji = to-2; for (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) {add (from, Hash1 (I, j), 1, 0 ); add (Hash2 (I, j), to, 1, 0); add (jiji, Hash2 (I, j), 1, 0); for (g = j + 1; g <= M; g ++) {ll cos = g-j-1; if (mp [I] [j] = mp [I] [g]) cos-= mp [I] [j]; add (Hash1 (I, j), Hash2 (I, g), 1, cos );} for (g = I + 1; g <= n; g ++) {ll cos = g-I-1; if (mp [I] [j] = mp [g] [j]) cos-= mp [I] [j]; add (Hash1 (I, j ), hash2 (g, j), 1, cos) ;}} add (from, jiji, k, 0); ll ans =-Mincost (from, ); if (flow! = N * m) puts ("-1"); else cout <ans <endl;} return 0 ;} /* 992 5 111111111112 5 211111111112 5 201110101012 5 310101021101 5 1009192910 10 1004545654101153132156451256412341564531045231354313256441345411313413243547896416512344686121213541550 */