http://codevs.cn/problem/1227/(Topic link)
Test instructions
The squares of n*n, each of which have a number, look for the K-path from (N,n), making the number and maximum taken.
Solution
The classic fee stream application.
Add the source point S and the meeting point T, respectively, with the (N,n) and (+) edges. Split each point in the chessboard into two, even two arcs. One of the capacity is 1, the cost is the number of the point, and the other is INF with a cost of 0. This means that the number of points can only be taken once, and then run the maximum cost of the maximum flow.
Details
Array Open Small:
Code
codevs1227#include<algorithm> #include <iostream> #include <cstring> #include <cstdlib># include<cstdio> #include <cmath> #include <queue> #define LL long long#define inf 2147483640#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout), using namespace std; const int maxn=100010;struct Edge {int from,to,next,c,w;} E[maxn<<1];int head[maxn],dis[maxn],vis[maxn],f[maxn],p[maxn];int cnt=1,n,m,es,et,k;void Link (int u,int v,int C,int W) {e[++cnt]= (edge) {u,v,head[u],c,w};head[u]=cnt;e[++cnt]= (edge) {v,u,head[v],-c,0};head[v]=cnt;} int SPFA () {queue<int> q;memset (dis,-1,sizeof (DIS)), Q.push (es);d is[es]=0;f[es]=inf;while (!q.empty ()) {int x= Q.front (); Q.pop (); vis[x]=0;for (int i=head[x];i;i=e[i].next) if (e[i].w && dis[e[i].to]<dis[x]+e[i].c) { Dis[e[i].to]=dis[x]+e[i].c;f[e[i].to]=min (F[X],E[I].W);p [E[i].to]=i;if (!vis[e[i].to]) Q.push (e[i].to), Vis[e[i]. To]=1;}} if (dis[et]==-1) return 0;for (int i=p[et];i;i=p[E[i].from]) E[i].w-=f[et],e[i^1].w+=f[et];return F[et]*dis[et];} int EK () {int ans=0;for (int i=1;i<=k;i++) ANS+=SPFA (); return ans;} int main () {scanf ("%d%d", &n,&k), es=n*n+1;et=n*n+2;for (int i=1;i<=n;i++) for (int x,y,w,j=1;j<=n;j++) { scanf ("%d", &w), x= (i-1) *n+j;y=x+n*n+2;link (x,y,w,1), Link (x,y,0,inf), if (i<n) Link (y,x+n,0,inf), if (j<n) Link (y,x+1,0,inf);} Link (es,1,0,inf), link (n*n+n*n+2,et,0,inf);p rintf ("%d", EK ()); return 0;}
"codevs1277" Check number 2