"The main topic"
n cattle from small to large rows, some distance between them can not be greater than a value, some distance can not be less than one value, to find the maximum distance between the first cow and the nth cow.
Ideas
The following inequalities can be obtained by test instructions d[al]+dl≥d[bl];d[bd]+ (-DD) ≥d[ad];d [I+1]+0≥d[i], which is obviously a differential constrained system. That is, constructs the edge from the AL to the BL weights to the DL, from the BD to the ad constructs the negative edge of the-DD, from the i+1 to the I constructs the weighted value to be 0. Finally, the shortest path is obtained. Amway A proof (point me).
For differential constrained systems, it is important to note that if the maximum distance is required, the shortest path is used, the minimum distance is used, and the longest path is determined according to the actual situation.
1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 Const intmaxn=100000+5;6 structRec7 {8 intOri,des,len;9 BOOL operator< (ConstRec &x)ConstTen { One returnLen>X.len; A } -}edge[maxn*5]; - intpar[maxn*2],height[maxn*2]; the intN,m,r; - /*N for female soldier, m for male soldiers, there is no need to use a binary map; R represents the number of relationships*/ - intans; - + voidInitset () - { + for(intI=0;i< (n+m); i++) A { atpar[i]=i; -height[i]=0; - } - } - - intFindintx) in { - intR=x,temp; to while(par[r]!=r) r=Par[r]; + while(x!=R) - { thetemp=Par[x]; *par[x]=R; $x=temp;Panax Notoginseng } - return(r); the } + A voidUnionset (intFaintFB) the { + if(height[fa]>=HEIGHT[FB]) - { $par[fb]=FA; $ if(HEIGHT[FA]==HEIGHT[FB]) height[fa]++; - } - Else thepar[fa]=FB; - }Wuyi the intMain () - { Wu intKase; -scanf"%d",&Kase); About for(intcases=0; cases<kase;cases++) $ { -scanf"%d%d%d",&n,&m,&R); - intans=10000* (m+n); - for(intI=0; i<r;i++) A { + intu,v,w; thescanf"%d%d%d",&u,&v,&W); -Edge[i].ori=u; $edge[i].des=v+N; theedge[i].len=W; the } theSort (edge,edge+R); the Initset (); - for(intI=0; i<r;i++) in { the intHasfind (Edge[i].ori); the intfb=find (edge[i].des); About if(fa!=FB) the { the Unionset (FA,FB); theans-=Edge[i].len; + } - } thecout<<ans<<Endl;Bayi } theSystem"Pause"); the return 0; -}
"Differential constrained system/SPFA" poj3169-layout