NYOJ61 (one) (Maximum cost flow solution)

Source: Internet
Author: User
Tags bool split
Description

Obuchi and Xiao Xuan are good friends and classmates, they always have to talk about endless topics. A quality expansion activities, the class arranged to make a M-row N-column matrix, and Obuchi and small Xuan is arranged at both ends of the matrix diagonal, so they can not directly talk. Fortunately, they can communicate by passing a note. The note to pass through many students to the other hand, Obuchi sitting in the upper left corner of the matrix, coordinates (at a), the small Xuan sits in the bottom right corner of the matrix, coordinates (M,N). From the Obuchi to the small Xuan note can only be passed down or to the right, from the small Xuan to Obuchi note can only be passed up or to the left.

In the activity, Obuchi hope to send a note to the small Xuan, at the same time hope that the small Xuan to reply to him. Every classmate in the class can help them pass, but will only help them once, that is, if the person in the Obuchi hand to the small Xuan Note when help, then in the small Xuan handed to Obuchi when will not help. Vice versa.

There is one more thing to pay attention to, the class each classmate is willing to help a high degree of good and low (note: Obuchi and small Xuan's kindness degree is not defined, input with 0), you can use a 0-1000 natural number to express, the greater the number of the more kindness. Obuchi and small Xuan hope as far as possible to find good-hearted students to help pass the note, that is, to find two ways to pass the route, so that the two path of the classmate's kindness degree and the largest. Now, please help Obuchi and Xiao Xuan to find such two paths. input

The first line inputs n (0 output

Each set of test data outputs a total of one row, containing an integer that represents the maximum value of the sum of kindness of the student who is involved in passing the note back and forth on both paths. Sample Input

1
3 3
0 3 9
2 8 5
5 7 0
Sample Output
34
Ideas

This problem was originally a two-line DP, did not expect to be able to use the cost of the flow to engage in. Two-line DP solution See here: NYOJ61 (a) (two-wire DP)

Network flow solution is the need to build a map, we ask for two not to cross the path from the top left to reach the bottom right, then we take each point to the left point and the right point, the map is as follows: The source point to the left point to the right to establish a capacity of 2, the cost of 0 of the edge (because it is two people) the left point of the meeting point to the right point to establish a capacity of 2, The side of the cost of 0 (because it is two persons) the left and right points of each point except the sink point and the source point establish a capacity of 1, the cost is the side of the kindness value a point to the right and down to establish a capacity of 1, the cost of 0 points

Then the maximum cost of the maximum flow, the specific method is that we only need to build the edge of the time, the value of negative values, so that the results obtained negative, is the maximum cost of the maximum flow. Code

#include <cstdio> #include <cstring> #include <cctype> #include <stdlib.h> #include <string&
Gt #include <map> #include <iostream> #include <stack> #include <cmath> #include <queue> #
Include <vector> #include <algorithm> using namespace std;
typedef long Long LL;
#define INF 1000000 #define MEM (A, B) memset (A,b,sizeof (a)) const int n=5000+20;

const int M=50000+20;
int top;//the current edge subscript int dis[n],pre[n];//The minimum distance to point I, pre[i] records precursor BOOL vis[n];//tag array int maxflow;
    int first[n];//storage head node struct Edge {int v,next;
int cap,flow,cost;

} E[m*2];
    void init () {mem (first,-1);
    Top=0;
maxflow=0;
    } void Add_edge (int u,int v,int c,int cost) {e[top].v=v;
    E[top].cap=c;
    e[top].flow=0;
    E[top].cost=cost;
    E[top].next=first[u];
first[u]=top++;
    } void Add (int u,int v,int c,int cost) {Add_edge (u,v,c,cost);
Add_edge (V,u,0,-cost);
    } bool SPFA (int s,int t,int n) {int i,u,v; Queue<int>Q
    MEM (Vis,false);
    MEM (pre,-1);
    for (int i=1; i<=n; i++) Dis[i]=inf;
    Vis[s]=true;
    dis[s]=0;
    Q.push (s);
        while (!q.empty ()) {U=q.front ();
        Q.pop ();
        Vis[u]=false;
            for (int i=first[u]; i!=-1; i=e[i].next) {v=e[i].v;
                if (E[i].cap>e[i].flow&&dis[u]+e[i].cost<dis[v]) {dis[v]=dis[u]+e[i].cost;
                Pre[v]=i;
                    if (!vis[v]) {Q.push (v);
                Vis[v]=true;
    }}}} if (Dis[t]==inf) return false;
return true;
    } int MCMF (int s,int t,int n)//mincostmaxflow {int D;
        int I,mincost=0;//maxflow Current maximum flow, mincost current minimum cost while (SPFA (s,t,n))//indicates that the minimum cost path {D=inf from S to T was found;
        for (int i=pre[t]; i!=-1; I=PRE[E[I^1].V])//Traverse reverse Edge d=min (d,e[i].cap-e[i].flow); maxflow+=d;//Update Max stream for (int i=pre[t]; i!=-1; i=PRE[E[I^1].V])//augmented road positive edge flow +d, reverse edge traffic-D {e[i].flow+=d;
        E[i^1].flow-=d;
} Mincost+=dis[t]*d;//dis[t] is the sum of the unit traffic charges on this path and the return mincost;
    } int main () {int t,n,m,x;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &m,&n);
        Init ();
        Add (0,1,2,0);
        Add (m*n-1), (m*n-1) +1,2,0);
                for (int. i=0; i<m; i++) for (int j=0; j<n; J + +) {scanf ("%d", &x); if (i>0) Add ((i-1) *n+j) +1,2* (i*n+j), 1,0),//When I is greater than 0 o'clock, create a left endpoint of the upper point after the break point and the next point if (j>0) Add (i*n +j-1) +1,2* (i*n+j), 1,0);//When j>0, create a split left point after the right endpoint and the right point of the left endpoint if (!) I==0 && j==0) &&!
                    (i = = M-1 && j = = n-1))
    Add (I*n+j), (i*n+j) +1,1,-x);//The split itself establishes a negative right side} printf ("%d\n",-MCMF (0,2* (m*n-1) +1,2* (m*n-1) +10));
} return 0;
 }

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.