Test instructions
1, a person from [the]->[n,n]->[1,1]
2, only can take the shortest
3, walk the point can not go again
Ask Max and.
For each point, the current limit of 1 can be 3.
Charge flow rate of 2 to meet 1
Maximum cost flow, the first to take negative, the result is negative, to meet 2
#include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <queue > #include <set> #include <algorithm> #include <stdlib.h> #define N 605*605*2#define M n*2#define inf 1<<29#define ll intusing namespace std;//bi-directional side, note re//Note that the point mark must be [0-meeting Point]struct edge{ll from, to, flow, cap, NEX, cos t;} Edge[m*2];ll Head[n], edgenum;void Add (ll u,ll v,ll cap,ll cost) {//network stream to be added reverse arc Edge e={u, V, 0, Cap, head[u], cost}; Edge[edgenum]=e; head[u]=edgenum++; Edge e2={v, u, 0, 0, head[v],-cost}; If the cap here is one-way side to 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 Mincost (ll S,ll t) {//return the minimum fee ll flow = 0, cost = 0; while (Bellmanford (s, t, flow, cost)); return cost;} void Init () {memset (head,-1,sizeof head); edgenum = 0;} ll N;ll Hash (ll X,ll y) {return (x-1) *n+y;} ll Hash2 (ll X,ll y) {return n*n+ (x-1) *n+y;} ll Mp[605][605];int Main () {ll I, J, u, V, cost; while (~SCANF ("%d", &n)) {init (); for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf ("%d", &mp[i][j]); for (i=1;i<=n;i++) {for (j=1;j<=n;J + +) {Add (Hash (i,j), Hash2 (I,J), 1,-mp[i][j]); U = HASH2 (i,j); if (i!=n) {v = Hash (i+1,j); Add (u,v,3,0); } if (j!=n) {v = Hash (i,j+1); Add (u,v,3,0); }}}add (hash (HASH2), 1, 0), add (hash (n,n), Hash2 (N,n), 1, 0); printf ("%d\n",-mincost (Hash), Hash2 (N,n)); } return 0;}
HDU 3376 && 2686 check maximum and cost flow bare topic