HDU 3376--matrix Again "maximum cost max Flow && Classic Build"

Source: Internet
Author: User

Matrix AgainTime limit:5000/2000 MS (java/others) Memory limit:102400/102400 K (java/others)
Total submission (s): 3457 Accepted Submission (s): 1020


Problem descriptionstarvae 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 starvae should to yes that choose a detour which from the top left point to the bottom right point and than B Ack to the top left point with the maximal values of sum integers this area of the Matrix starvae 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 starvae can not pass the same area of the Matrix except the start and end.
Do you know why call this problem as "Matrix Again"? As it is like the problem 2686 of HDU.

Inputthe input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)

Outputfor each test case output the maximal values starvae 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

Test instructions

Give you a n*n matrix, where each element represents the weight of the place. Requires that each point can only walk once, the upper-left and lower-right corner can walk two times but the weight of the value can only be obtained once. Ask you to go from the upper left corner to the lower right corner (can only move down or right), and then from the lower right corner to the upper left corner (can only move up or left) to get the maximum weight.

Analytical:

The question asks from the upper left corner to the lower right corner, and then from the lower right corner back to the upper left corner can get the maximum weight, we can convert from the "top left" to "the bottom right corner" to walk two times the maximum weight obtained. The topic requires the beginning of the end can go many times, others must only walk once. In this way, the beginning and the end of the weight of the value of our more than once, the final result to be subtracted.

Think of the graph as n * n points, between the points that can be reached between the building edge, the number of times as the capacity of the edge, the weight as the cost of the edge. The problem becomes the maximum cost flow.

The process of building the map:

Each point I is split into left point I and right point i + N * N. Virtual One super Source point outset = 0, super sink point inset = 2 * n * n + 1.

(1) When I is the beginning or the end point, the left point to the right to build the edge, the capacity of the edge is 2, because the starting point can walk two times, the cost of the edge is the weight of the point.

(If the starting point and the end of the 1-side, then we have only one choice at the starting point: go up or down, at the end of the point can only choose to be from the above points to the end or from the left point to the end, so that is the equivalent of the maximum weight to go once and, obviously this map is wrong.) )

(2) When I is not the starting point or the end point, the left point to the right points to build the edge, the capacity of the edge is 1, because only walk once, the cost of the side is the weight of the point.

(3) A mutually reachable point between the building edge, such as U-V, the building edge of the right point U and left Point v built Edge, the edge of the minimum capacity of 1, the cost of the edge is 0. This is to understand!!!

(4) The source point to the starting point of the left side of the building edge, the capacity of the edge is 2, the cost of the edge is 0, the end of the right point to the meeting point building edge, the edge of the capacity of 2, the cost is 0.


This kind of problem gives you a n*m matrix, corresponding to n*m points, and each point has a certain point of the right. When we arrive at a location for the first time, we can get the point of that location right vote can only get this once.

There are two types of situations:

(1) The requirement is that each point can only walk once (in addition to the beginning and the end can walk multiple times), ask you from the upper left to the lower right corner to walk two times to obtain the maximum weight and.

(2) The requirement is that each point can go multiple times, ask you to go from the upper left corner to the lower right corner of the K-Times to get the maximum weight and.

The situation (1) should be able to build the diagram in this way.

Situation (2) has POJ 3422 questions, POJ3422.


Build the steps there is nothing to understand the place, you can read this blog: Little than the blog. It also said the situation (2). (I have drawn a morning figure to understand, I know the blog to see small than, can save a lot of time, there is a giant shoulder can stand is good).

#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)) {//each time looking for the least cost path 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);            }//augmented 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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3376--matrix Again "maximum cost max Flow && Classic Build"

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.