Matrix (maximum cost of hdu 2686 Stream)

Source: Internet
Author: User

Matrix (maximum cost of hdu 2686 Stream)

 

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



Problem Description Yifenfei 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.

Input The input contains multiple test cases.
Each case first line given the integer n (2 Than n lines, each line include n positive integers. (<100)

Output For 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

Author yifenfei
Source ZJFC 2009-3 Programming Contest
Recommend yifenfei | We have carefully selected several similar problems for you: 3376 2448 3395 3491
Question: a matrix of n * n is given. Each vertex has a value. Now, from the upper left corner, a path is taken to the lower right foot (only right or down ), then return to the upper left corner (only left or up) from the lower right corner. In this process, each vertex can only be taken once. How much is the maximum sum of the weights on the path?

 

Idea: here we use the cost stream solution. First we add a super Source Vertex s = 0 and super sink vertex t = n * n + 1, and then split each vertex, i-to-I 'edge, capacity is 1, cost is the weight of this point mp [I] [j], then s and 1' edge, capacity is 2, the cost is 0, n * n to the t edge, the capacity is 2, the cost is 0, and the last point in the matrix is connected to the edge, the capacity is 1, the cost is 0. The final answer is cost + mp [1] [1] + mp [n] [n]. Pay attention to the size of the array.

Code:

 

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Include
       # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Pragma comment (linker,/STACK: 102400000,102400000) # define mod 1000000009 # define INF 0x3f3f3f3f # define pi acos (-1.0) # define eps 1e-6 # define lson rt <1, l, mid # define rson rt <1 | 1, mid + 1, r # define FRE (I, a, B) for (I = a; I <= B; I ++) # define FREE (I, a, B) for (I = a; I> = B; I --) # define FRL (I, a, B) for (I = a; I <B; I ++) # define FRLL (I, a, B) for (I = a; I> B; I --) # define mem (t, v) memset (t), v, sizeof (t) # define sf (n) scanf (% d, & n) # define sff (a, B) scanf (% d, & a, & B) # define sfff (a, B, c) scanf (% d, & a, & B, & c) # define pf printf # define DBG pf (Hi) typedef long ll; using namespace std; const int MAXN = 2000; // note the array size const int MAXM = 100000; struct Edge {int to, next, cap, flow, cost;} edge [MAXM]; int head [MAXN], tol; int pre [MAXN], dis [MAXN]; bool vis [MAXN]; int N, n, m; void init (int n) {N = n; tol = 0; memset (head,-1, sizeof (head);} void addedge (int u, int v, int cap, int cost) {edge [tol]. to = v; edge [tol]. cap = cap; edge [tol]. cost = cost; edge [tol]. flow = 0; edge [tol]. next = head [u]; head [u] = tol ++; edge [tol]. to = u; edge [tol]. cap = 0; edge [tol]. cost =-cost; edge [tol]. flow = 0; edge [tol]. next = head [v]; head [v] = tol ++;} bool spfa (int s, int t) {queue
            
              Q; for (int I = 0; I
             
               Edge [I]. flow & dis [v] <dis [u] + edge [I]. cost) {dis [v] = dis [u] + edge [I]. cost; pre [v] = I; if (! Vis [v]) {vis [v] = true; q. push (v) ;}}}if (pre [t] =-1) return false; else return true;} int minCostMaxflow (int s, int t, int & cost) {int flow = 0; cost = 0; while (spfa (s, t) {int Min = INF; for (int I = pre [t]; i! =-1; I = pre [edge [I ^ 1]. to]) {if (Min> edge [I]. cap-edge [I]. flow) Min = edge [I]. cap-edge [I]. 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;} return flow;} int mp [MAXN] [MAXN]; int main () {# ifndef ONLINE_JUDGE freopen (C: /Users/asus1/Desktop/IN.txt, r, stdin); # endif int I, j, t; while (~ Sf (n) {init (2 * n + 2); FRE (I, 1, n) FRE (j, 1, n) sf (mp [I] [j]); addedge (0, 1 + n * n, 2, 0); addedge (n * n, 2 * n + 1, 2, 0 ); FRE (I, 1, n) FRE (j, 1, n) {addedge (I-1) * n + j, (I-1) * n + j + n * n, 1, mp [I] [j]); if (j + 1 <= n) addedge (I-1) * n + n * n + j, (I-1) * n + j +, 0); if (I + 1 <= n) addedge (I-1) * n + j + n * n, I * n + j, 1, 0);} int cost; int ans = minCostMaxflow (0, N-1, cost); printf (% d, cost + mp [1] [1] + mp [n] [n]);} return 0 ;}
             
            
           
          
         
        
      
     
    
   
  


 

Related Article

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.