HDU 5406 CRB and Apple pattern cost flow Solution

Source: Internet
Author: User

Test instructions Simplification is a sequence that finds two of the largest non-descending sub-sequences making them lengths and longest.

= = as a DP slag, the state design is probably to the dp[i][j] means the first person to take the last I, the second person took to j this place. How to transfer within the feasible complexity? No, look at the sky.

In fact, as a graph theory workers first reaction is the cost of flow, but the number of sides too many do not dare to engage in =

However, the cost flow can also be too. (x, Y) indicates that the capacity is X cost for Y,

It is simple to build a source s and sink E, plus a point t limit the maximum flow from the source is 2, which is s->t (2, 0). Remove each point can only be taken once, x->x ' (1, 1), capacity 1 limit can only be taken once, the cost represents the length of +1

T->x (1, 0) can start at any point. X '->e (1, 0) can end at any one point.

Run the maximum cost of the maximum flow and then it will be T (hello)

Remember POJ a problem card SPFA, but change into a stack can be over = =

So you can change it to a stack and find that it's not T. (From the outset, this problem has been bothering me. T-t How to construct the card queue not stack data? T-T for guidance)

Code 1, about 1400ms.

#include <cstdio>#include<cstring>#include<algorithm>#include<queue>#include<stack>using namespacestd;Const intN = .;Const intM =4000005;Const intINF =0x3f3f3f3f;structpoint{intu,v, Next, flow, cost;    Point () {}; Point (intXintYintZintFintc) {u= x, v= y; Next = Z, flow = f, cost =C; };} P[M];intPre[n], head[n], d[n];BOOLVis[n];intS, E, no;structinfo{intH, D; voidScan () {scanf ("%d%d", &h, &d); }    BOOL operator< (ConstInfo &i)Const{        if(h = = I.h)returnD <I.D; returnH >I.h; }}info[n];BOOLSPFA () {stack<int>Q; intx, Y, now, I; memset (d,0xc0,sizeof(d)); memset (Vis,0,sizeof(VIS)); memset (PRE,-1,sizeof(pre));  Q.push (s); D[s]=0; Vis[s] =1;  while(!Q.empty ()) {x=Q.top ();        Q.pop (); VIS[X]=0;  for(i = head[x]; I! =-1; i =P[i].next) {y=p[i].v; if(P[i].flow && D[y] < d[x] +p[i].cost) {D[y]= D[x] +P[i].cost; Pre[y]=i; if(!Vis[y])                    {Q.push (y); Vis[y]=1; }            }        }    }    return(D[e]! = d[e+1]);}intMCMF () {intMaxflow =0, I, minflow, mincost =0;  while(SPFA ()) {Minflow= INF +1;  for(i = pre[e]; I! =-1; i =PRE[P[I].U]) {            if(P[i].flow <minflow) Minflow=P[i].flow; }         for(i = pre[e]; I! =-1; i =PRE[P[I].U]) {P[i].flow-=Minflow; P[i^1].flow + =Minflow; } mincost+ = d[e] *Minflow; }    returnMincost;}voidinit () {memset (head,-1,sizeof(head)); No=0;}voidAddintXintYintFintCO) {P[no]= Point (x, Y, head[x], F, CO); HEAD[X] = no++; P[no]= Point (Y, X, Head[y],0,-co); Head[y] = no++;}intMain () {intTC, N, I, J; scanf ("%d", &TC);  while(tc--) {scanf ("%d", &N); Init (); s=0; E =2* n +2;  for(i =1; I <= N; i++) {Info[i].scan (); Add (i<<1, I <<1|1,1,1); Add (1, I <<1,1,0); Add (i<<1|1E1,0); } Sort (Info+1, info + n +1);  for(i =1; I <= N; i++){             for(j = i +1; J <= N; J + +){                if(INFO[I].D <=info[j].d) {Add (i<<1|1, J <<1,1,0); }}} Add (S,1,2,0); printf ("%d\n", MCMF ()); }    return 0;}
View Code

Then proceed to the various optimization stages.

The first attempt to improve the way to build the edge, specifically see http://blog.csdn.net/mxymxy1994mxy/article/details/47818397 the great God, the space saved a lot, However, in the ordinary queue or T (of course, it may be my stupid (but obviously not possible, in fact, is that I am stupid, lying (why do you want to vomit))), simple form is a strong orz, but to the stack of SPFA, less than 500ms, is bad ah.

Code 2, the others are the same, modified the way the edge is built

Init (); s =0; E =2* n +2;  for(i =1; I <= N; i++) {Info[i].scan (); Add (i<<1, I <<1|1,1, -1); Add (1, I <<1,1,0); Add (i<<1|1E1,0); } Sort (Info+1, info + n +1); intMAXN;  for(i =1; I <= N; i++) {MAXN=INF;  for(j = i +1; J <= N; J + +){                if(Info[j].d <info[i].d) {                    Continue; }                if(INFO[J].D <=MAXN) {Add (i<<1|1|1, J <<1,1,0); Add (i<<1, J <<1,1,0); //flag = false;MAXN =INFO[J].D; }}} Add (S,1,2,0);
View Code

Get out and eat dinner and go back to the lab and try all the solutions.

HDU 5406 CRB and Apple pattern cost flow Solution

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.