Hdoj 3376 Matrix Again and hdoj 2686 matrix "maximum cost maximum flow"

Source: Internet
Author: User



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


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



no words died, tle to death. Fortunately finally with g++ Card, C + + die.


Two test instructions is the same, the data card is not the same!!!

Test instructions: give you a n*n matrix, 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.


Idea: To n*n all the elements into a point, the relationship as an edge, the number of times as the capacity of the edge, the weight as the cost of the side. The problem becomes the maximum cost flow.


Build: Set Super Source point sink, super sinks source

1, all points I split the left point I and the right point I + N*n,i to i+n*n, the capacity is 1 (only if I is the starting point or the end of the capacity is 2), the cost is the point right.

The left point of the upper left corner of the 2,sink connection, the capacity is 2, the cost 0;

3, right bottom right point connection source, capacity is 2, cost 0;

4, all accessible relationships, i.e. for coordinates (x, y) to (x+1, y) and (x, y+1) are built (boundaries need to be discussed), capacity is 1 (at least 1 can be greater than 1), cost 0.

Finally run the maximum cost after the maximum flow, minus the starting weight and the end of the weight (we forget more once) is OK. Understanding the minimum cost to solve the maximum cost is simple, just change the SPFA process to find the maximum cost path for s-t.


AC Code: Can cross these two questions


#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <stack># Include <algorithm> #define MAXN 800000+10#define maxm 4000000+10#define INF 0x3f3f3f3fusing namespace Std;struct edge{int from, to, cap, flow, cost, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int PRE[MAXN], dist[maxn];bool vis[maxn];int n;int Map[610][610];int sink,    Source;void init () {edgenum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w, int c) {edge[edgenum].from = u;    Edge[edgenum].to = v;    Edge[edgenum].cap = W;    Edge[edgenum].flow = 0;    Edge[edgenum].cost = C;    Edge[edgenum].next = Head[u];    Head[u] = edgenum++;    Edge[edgenum].from = v;    edge[edgenum].to = u;    Edge[edgenum].cap = 0;    Edge[edgenum].flow = 0;    Edge[edgenum].cost =-C;    Edge[edgenum].next = Head[v]; HEAD[V] = edgenum++;} int point (int x, int y) {return (x-1) *n + y;}    void Getmap () {int k = n*n; sink = 0;    Source = 2*k+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) Addedge (Point (I, J), Point (I, J) + K, 2  , Map[i][j]);//start and end capacity is 2 cost for point right else Addedge (points (I, j), Dot (i, j) + K, 1, map[i][j]);//left Dot even right point            The capacity for the 1 charge is the point right if (I < N) Addedge (points (i, J) +k, Point (I+1, J), 1, 0);//Right point with left dot capacity 1 cost 0        if (J < N) Addedge (Point (I, J) +k, point (I, j+1), 1, 0); }} addedge (sink, 1, 2, 0);//The left point capacity of the starting point of the super Source point 2 cost is 0 Addedge (point (N, N) +k, source, 2, 0);//end of the right-click Super Meeting point Capacity 2 fee is 0}boo    L SPFA (int s, int t) {queue<int> Q;    memset (Dist,-inf, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    memset (Pre,-1, sizeof (pre));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = false; for (int i= Head[u]; I! =-1;            i = Edge[i].next) {Edge E = Edge[i]; if (Dist[e.to] < Dist[u] + e.cost && e.cap > E.flow)//maximum cost and no full stream path {dist[e.to] = d                Ist[u] + e.cost;                Pre[e.to] = i;                    if (!vis[e.to]) {vis[e.to] = true;                Q.push (e.to); }}}} ' return pre[t]! =-1;}    void MCMF (int s, int t, int &cost, int &flow) {cost = flow = 0;        while (SPFA (s, t)) {int Min = INF;            for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {Edge E = Edge[i];        min = min (min, e.cap-e.flow);            } for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {edge[i].flow + = Min;            Edge[i^1].flow = Min;        Cost + = Edge[i].cost * Min;    } flow + = Min;        }}int Main () {while (scanf ("%d", &n)! = EOF) {init (); Getmap();        int cost, flow;        MCMF (sink, source, cost, flow);    Cost-= Map[1][1] + map[n][n];//The value of the start and end of the printf ("%d\n", and the cost); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 3376 Matrix Again and hdoj 2686 matrix "maximum cost maximum flow"

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.