HDU 3376 -- Matrix Again [maximum cost flow & amp; classic diagram], hdu3376 -- matrix

Source: Internet
Author: User

HDU 3376 -- Matrix Again [maximum cost flow & Classic Graph creation], hdu3376 -- matrix

Matrix Again Time 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 shocould to do is that choose a detour which from 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 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 before t 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
 

Question:

Give you a matrix of N * N. Each element represents the weight of this place. Each vertex must be taken only once. The upper left corner and lower right corner can be taken twice, but the weight of the vertex can only be obtained once. Ask the maximum weight you can get from the upper left corner to the lower right corner (only move down or right), and then back to the upper left corner (only move up or left.

Resolution:

Question: go from the upper left corner to the lower right corner, and then return to the upper left corner to obtain the maximum weight, we can convert the value to the maximum weight obtained twice from the starting point in the upper left corner to the ending point in the lower right corner. The question requires that the start point and end point can be taken multiple times, and the rest can only be taken once. In this case, we will calculate the weight of the start and end points one more time, and the final result will be subtracted.

Consider a graph as n * n points, and build edges between reachable points. The number of walk times is used as the edge capacity, and the weight is used as the edge cost. The problem becomes the maximum charge flow.

Graph creation process:

Split each vertex I into the left vertex I and the right vertex I + n * n. Virtual super source point outset = 0, super sink point inset = 2 * n + 1.

(1) When I is the start point or the end point, the edge is built from the left to the right, and the size of the edge is 2, because the start and end points can be taken twice, and the edge cost is the weight of the point.

(If the start point and the end point are split into an edge with a capacity of 1, we have only one choice at the start point: go up or down, at the end point, you can only select whether the above vertex is uploaded to the end point or the left vertex is passed to the end point. This is equivalent to obtaining the maximum weight sum once, obviously, creating a graph is incorrect .)

(2) When I is not the start point or the end point, the edge is built at the right of the left point. The edge capacity is 1 because it can only be taken once, And the edge cost is the vertex weight.

(3) create an edge between mutually reachable points, such as u --> v. When creating an edge, create an edge at the right point u and the left point v. The minimum edge capacity is 1, the cost of edge is 0. You need to understand this !!!

(4) Create an edge at the left of the Source Vertex to the start vertex. The edge capacity is 2, the edge cost is 0, and the right vertex to the end vertex to build an edge. The edge capacity is 2, the fee is 0.


This type of question gives you a matrix of N * M, corresponding to N * M points, and each point has a certain vertex weight. When we reach a position for the first time, we can obtain the point right of this position and can only obtain this time.

There are two scenarios:

(1) The requirement is that each vertex can only go once (except the start and end points can be walked multiple times), and you are asked to go from the top left to the bottom right corner twice to obtain the maximum weight and.

(2) The request is that each vertex can be walked multiple times and you are asked about the maximum weight and value obtained K times from the upper left corner to the lower right corner.

(1) You can use this method to create a graph.

(2) There are POJ 3422 questions and POJ3422 questions.


If you do not understand the graph creation process, read this blog: xiaobi's blog. It also describes the situation (2 ). (After drawing a picture for a whole morning, I want to understand that I knew that I had been watching the mini-byte blog, saving a lot of time. It would be nice to have a giant on the shoulder to stand ).

# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # define INF 0x3f3f3f3f # define maxn 800000 + 1000 # define maxm 4000000 + 1000 using 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 * 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; pe R [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 )) {// The 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 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.