codevs_1227 Check number 2 network flow minimum cost flow split

Source: Internet
Author: User

Original title Link: http://codevs.cn/problem/1227/

Title Description Description

Give a n*n matrix, each lattice has a non-negative integer Aij, (Aij <= 1000) Now from (the first), you can go to the right or down, and finally arrive (N,n), each reached a lattice, the number of the lattice out, the number of the lattice into 0, so that a total of k times, The number of squares that are now required to reach the K-times and the maximum

Enter a description input Description

First row two numbers n,k (1<=n<=50, 0<=k<=10)

The next n rows, the number of n per row, each of the squares of the matrix.

outputs description output Description

A number, for the most Yamato

sample input to sample

3 1

1 2 3

0 2 1

1 4 2

Sample output Sample outputs

11

data size & Hint

1<=N<=50, 0<=k<=10


This problem is a very bare split the minimum cost flow, each point is opened after the construction of two side, a fee is-a[i][j], the capacity of 1, the other cost is 0, the capacity of the INF. The remaining costs are 0, the capacity is the INF edge connection, and each point is connected to the sink point. The inverse of the last minimum charge flow is the answer. See the code:

#include <iostream> #include <vector> #include <cstring> #include <algorithm> #include < string> #include <queue> #include <set> #define MAX_N 55#define max_v 6000#define INF 1008611using namespace Std;int k,n;int a[max_n][max_n];struct edge{int To,cap,cost,rev;}; int v=0;vector<edge> g[max_v];int dist[max_v];int prevv[max_v],preve[max_v];void add_edge (int from,int to,int    Cap,int cost) {G[from].push_back (edge) {to,cap,cost,g[to].size ()}); G[to].push_back (Edge) {from,0,-cost,g[from].size ()-1});}    Char cc;int min_cost_flow (int s,int t,int f) {int res=0;        while (f>0) {fill (dist,dist+v,inf);        dist[s]=0;        BOOL update=1;            while (update) {update=0;                for (int v=0;v<v;v++) {if (dist[v]==inf) continue;                    for (int i=0;i<g[v].size (); i++) {Edge &e=G[v][i]; if (e.cap>0&&dist[e.to]>dist[v]+e. Cost) {//cout<< "*" <<endl;                        Dist[e.to]=dist[v]+e.cost;                        Prevv[e.to]=v;                        Preve[e.to]=i;                    update=1;        }}}} if (Dist[t]==inf) return-1;        int d=f;        for (int v=t;v!=s;v=prevv[v]) d=min (D,G[PREVV[V]][PREVE[V]].CAP);        F-=d;        RES+=D*DIST[T];            for (int v=t;v!=s;v=prevv[v]) {Edge &e=G[prevv[v]][preve[v]];            E.cap-=d;        G[v][e.rev].cap+=d; }} return res;}    int main () {cin>>n>>k;    for (int i=0;i<n;i++) for (int j=0;j<n;j++) cin>>a[i][j];    v=n*n*2+1;            for (int i=0;i<n;i++) for (int j=0;j<n;j++) {int v= (i*n+j) * *;            int u=v+1;            Add_edge (V,u,1,-a[i][j]);            Add_edge (v,u,inf,0); if (i!=n-1) Add_edge (U, ((i+1) *n+j) *2,inf,0);            if (j!=n-1) Add_edge (u,u+1,inf,0);        Add_edge (u,v-1,inf,0);    } cout<<-min_cost_flow (0,v-1,k) <<endl; return 0;}


codevs_1227 Check number 2 network flow minimum cost flow split

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.