Codevs 1227 squares Fetch 2 (expense flow)

Source: Internet
Author: User
Tags bool split first row time limit
1227 Squares Fetch number 2

time limit: 1 s space limit: 128000 KB topic rank: Master Master

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 maximum and

sample input to sample

3 1

1 2 3

0 2 1

1 4 2

sample output Sample outputs

One

Data Size & Hint

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


"Solving" " Network Flow fee Flow"

"Build: Split Point, a point is split into two, two points between two edges, an edge flow of 1, the weight of the number of squares (indicating that each number can only be taken once); the other side traffic is the number of trips K, the weight of 0 (that is, each square can go multiple times, but the number can only be taken once) Then add the source point and the sink point, the source point to (the) edge of the traffic is K, the weight value is 0, (n,n) to the edge of the meeting point of the traffic is K, the weight value is 0 "

"consider that the initial value of the DIS array should be assigned to a minimum, and then gradually to the big update"

#include <queue> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
int a[50100],next[50100],p[5010],remain[50100],w[50100],tot=-1;
int dis[50100],last[50100];
int n,k,maxflow,mincost,n1,tt;

BOOL b[50100];
  inline void Add (int x,int y,int z,int cost) {tot++; a[tot]=y; next[tot]=p[x]; p[x]=tot; remain[tot]=z; w[tot]=cost; tot++; A[tot]=x; Next[tot]=p[y]; P[y]=tot; remain[tot]=0;
  W[tot]=-cost;
Return
  } inline int Addflow (int s,int t) {int sum=0x7fffffff,now=t;
    while (now!=s) {sum=min (Sum,remain[last[now]]);
   NOW=A[LAST[NOW]^1];
  } now=t;
     while (now!=s) {remain[last[now]]-=sum;
     Remain[last[now]^1]+=sum;
   NOW=A[LAST[NOW]^1];
} return sum;
    } inline bool Spfa (int s,int t) {int x;
    memset (dis,128,sizeof (dis));
    Memset (b,true,sizeof (b)); queue<int>que;
    X=dis[0]; B[s]=false; dis[s]=0;
    Que.push (s);
        while (!que.empty ()) {int u,v; U=que.front (); QUe.pop (); V=p[u];
        B[u]=true; while (V!=-1) {if (Remain[v]&&dis[a[v]]<dis[u]+w[v]) {Dis[a [V]]
                =DIS[U]+W[V];
                Last[a[v]]=v;
                    if (B[a[v]]) {b[a[v]]=false;
                Que.push (A[v]);
         }} V=next[v];
    }} if (dis[t]<0) return false;
    int Ans=addflow (S,T);
    Maxflow+=ans;
    Mincost+=dis[t]*ans;
return true;
    } int main () {int i,j;
    memset (P,-1,sizeof (p));
    memset (Next,-1,sizeof (next)); Tot=-1;
    tt=0;
    scanf ("%d%d", &n,&k);
    N1=n*n;
        for (I=1;i<=n;++i) for (j=1;j<=n;++j) {int x;
        scanf ("%d", &x);
        tt++;
        Add (tt,tt+n1,1,x);
        Add (tt,tt+n1,k,0);
        if (i!=n) Add (tt+n1,tt+n,k,0);
    if (j!=n) Add (tt+n1,tt+1,k,0); } n1*=2;
    n1++;
    Add (0,1,k,0);
    Add (n1-1,n1,k,0);while (SPFA (0,N1));
    printf ("%d", mincost); 
return 0;  }


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.