Poj Kaka's matrix travels

Source: Internet
Author: User

KaKa's matrix travels

 

Question:

A matrix is given to find the largest sum that can be obtained only when the value is downward or right. Generally, traversal is performed once, which can be repeated for K times. After each passing operation, set this point to 0. Calculate the largest sum.

 

Algorithm:

I thought of using network streams. However, you are not confident in creating images. After reading other people, you dare to start building. It is not difficult to create a graph, that is, there is a split point processing, because the value on it is 0 after one point is taken once. This processing is clever! It is to create two sides after splitting. One is a valuable edge, and the other is a non-valuable edge, but it can pass through. Although this point is of no value, other points may need to be passed through it. This is the clever point of this question !!! Think twice .....

A graph creation model drawn by others is well analyzed.

 

#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <cstdio>#include <cstring>using namespace std;const int INF = 1 << 25;const int MAXN = 5000 + 10;////////////////////////////////费用流struct Edge{    int from,to,cap,flow,cost;    Edge(){};    Edge(int _from,int _to,int _cap,int _flow,int _cost)        :from(_from),to(_to),cap(_cap),flow(_flow),cost(_cost){};};vector<Edge> edges;vector<int> G[MAXN];bool inq[MAXN];int d[MAXN];int p[MAXN];int a[MAXN];int N,K,V,src,sink;/////////////////////////////int matrix[MAXN][MAXN];void init(){    src = N * N * 2; sink = src + 1;    for(int i = 0; i < sink + 1;++i)        G[i].clear();    edges.clear();}void addEdge(int from,int to,int cap,int cost){    edges.push_back(Edge(from,to,cap,0,cost));    edges.push_back(Edge(to,from,0,0,-cost));    int sz = edges.size();    G[from].push_back(sz - 2);    G[to].push_back(sz - 1);}bool spfa(int s,int t,int& flow,int& cost){    for(int i = 0;i <= sink;++i) d[i] = INF;    fill(inq,inq + V,false);    d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;    queue<int> Q;    Q.push(s);    while(!Q.empty()){        int u = Q.front(); Q.pop();        inq[u] = false;        for(int i = 0;i < (int)G[u].size();++i){            Edge& e = edges[G[u][i]];            if(e.cap > e.flow && d[e.to] > d[u] + e.cost){                d[e.to] = d[u] + e.cost;                p[e.to] = G[u][i];                a[e.to] = min(a[u],e.cap - e.flow);                if(!inq[e.to]){                    inq[e.to] = true;                    Q.push(e.to);                }            }        }    }    if(d[t] == INF)        return false;    flow += a[t];    cost += d[t] * a[t];    int u = t;    while(u != s){        edges[p[u]].flow += a[t];        edges[p[u]^1].flow -= a[t];        u = edges[p[u]].from;    }    return true;}int minCost(){    V = sink + 1;    int flow = 0,cost = 0;    while(spfa(src,sink,flow,cost));    return cost;}int main(){//    freopen("Input.txt","r",stdin);    while(~scanf("%d%d",&N,&K)){        init();        for(int i = 0;i < N;++i){            for(int j = 0;j < N;++j){                scanf("%d",&matrix[i][j]);            }        }        V = N*N;        int t;        for(int i = 0;i < N;++i){          //拆点            for(int j = 0;j < N;++j){                t = i * N + j;                addEdge(t,t + V,1,-matrix[i][j]);  //要这点的价值                addEdge(t,t + V,INF,0);          //其他点可以从这点过            }        }        int t1,t2;        for(int i = 0;i < N - 1;++i){   //向下建边            for(int j = 0;j < N;++j){               t1 = i * N + j;               t2 = (i + 1) * N + j;               addEdge(t1 + V,t2,INF,0);            }        }        for(int i = 0;i < N;++i){      //向右建边            for(int j = 0;j < N - 1;++j){                t1 = i * N + j;                t2 = i * N + j + 1;                addEdge(t1 + V,t2,INF,0);            }        }        addEdge(src,0,K,0);      //超级源点        addEdge(2*V - 1,sink,K,0);  //超级汇点        printf("%d\n",-minCost());    }    return 0;}


 

Poj Kaka's matrix travels

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.