HPU 2686 -- Matrix [maximum cost flow & amp; Classic Graph creation], hpu2686 -- matrix

Source: Internet
Author: User

HPU 2686 -- Matrix [maximum cost flow & Classic Graph creation], hpu2686 -- matrix

Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2062 Accepted Submission (s): 1074


Problem DescriptionYifenfei very like play a number game in the n * n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei shocould to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area matrix yifenfei choose. but from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. and yifenfei can not pass the same area of the Matrix before t the start and end.
 
InputThe input contains multiple test cases.
Each case first line given the integer n (2 <n <30)
Than n lines, each line include n positive integers. (<100)
 
OutputFor each test case output the maximal values yifenfei can get.
Sample Input
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
 
Sample Output
284680
 

It is exactly the same as HPU3376, but the data is weak and decisive.

For details, see HPU3376.

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define maxn 800000 + 1000#define maxm 4000000 + 1000using namespace std;int n;int outset;int inset;struct node {    int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int per[maxn];int dist[maxn], vis[maxn];int map[660][660];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){    node E1 = {u, v, w, 0, c, head[u]};    edge[cnt] = E1;    head[u] = cnt++;    node E2 = {v, u, 0, 0, -c, head[v]};    edge[cnt] = E2;    head[v] = cnt++;}int change(int x, int y){    return (x - 1) * n + y;}void getmap(){    int t = n * n;    outset = 0;    inset = n * n * 2 + 1;    for(int i = 1; i <= n; ++i){        for(int j = 1; j <= n; ++j){            scanf("%d", &map[i][j]);            if(i == 1 && j == 1 || i == n && j == n)                add(change(i, j), change(i, j) + t, 2, map[i][j]);            else                add(change(i, j), change(i, j) + t, 1, map[i][j]);            if(i + 1 <= n)                add(change(i, j) + t, change(i + 1, j), 1, 0);            if(j + 1 <= n)                add(change(i, j) + t, change(i, j + 1), 1, 0);        }    }    add(outset, 1, 2, 0);    add(change(n, n) + t, inset, 2, 0);}bool SPFA(int st, int ed){    queue<int>q;    for(int i = 0; i <= inset; ++i){        dist[i] = -INF;        vis[i] = 0;        per[i] = -1;    }    dist[st] = 0;    vis[st] = 1;    q.push(st);    while(!q.empty()){        int u = q.front();        q.pop();        vis[u] = 0;        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(dist[E.v] < dist[u] + E.cost && E.cap > E.flow){                dist[E.v] = dist[u] + E.cost;                per[E.v] = i;                if(!vis[E.v]){                    vis[E.v] = 1;                    q.push(E.v);                }            }        }    }    return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){    flow = 0;    cost = 0;    while(SPFA(st, ed)){        int mins = INF;        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            mins = min(mins, edge[i].cap - edge[i].flow);        }        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            edge[i].flow += mins;            edge[i ^ 1].flow -= mins;            cost += edge[i].cost * mins;        }        flow += mins;    }}int main (){    while(scanf("%d", &n) != EOF){        init();        getmap();        int cost, flow;        MCMF(outset, inset, cost, flow);        cost = cost - map[1][1] - map[n][n];        printf("%d\n", cost);    }    return 0;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.