Give us n house, the height of the house is different, starting from the lowest house, each jump to a higher house, jumping n-1 times the most can jump to the highest house, but the distance of each jump can not exceed D
Rearrange the houses in one-dimensional direction (but keep the relative position of the inputs unchanged), making the shortest house and the tallest house horizontal at the maximum
Set the coordinates of the House to Xi, n variables, and 2 (n-1) constraints, a typical differential constraint system
The height of the two coordinates (set to XI,XJ) subtract abs (XI-XJ) <= D, to find a way to remove the absolute value, then specify the ID of the large minus ID, then the result is positive
Also need to keep the relative position of the input unchanged, then X (i+1)-X (i) >=1, because to be the shortest circuit so is x (i)-X (i+1) <= 1
So follow the above analysis to construct the diagram, and then run one side of the shortest possible.
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<queue>#include<stack>#include<vector>#include<map>#include<Set>#include<string>#include<math.h>using namespacestd;#pragmaWarning (disable:4996)#pragmaComment (linker, "/stack:1024000000,1024000000")typedefLong LongLL; Const intINF =1<< -;/**/Const intN = ++Ten;structnode{intID, height; BOOL operator< (ConstNode &RHS) { returnHeight <Rhs.height; }}a[n];structedge{intV, Dist; Edge () {} Edge (int_v,int_dist): V (_v), dist (_dist) {}BOOL operator< (ConstEDGE&RHS)Const { returnDist >rhs.dist; }};vector<Edge>G[n];intDist[n];BOOLVis[n];intDij (intXintYintN) { for(inti =1; I <= N; ++i) dist[i]=INF; Priority_queue<Edge>Q; Edge cur, tmp; Cur.dist= Dist[x] =0; CUR.V=x; Q.push (cur); while(!Q.empty ()) {cur=q.top (); Q.pop (); intU =CUR.V; if(Dist[u] < cur.dist)//if Cur.dist < Dist[u], you can continue to update other vertices instead of the condition Vis[u] Continue; for(inti =0; I < g[u].size (); ++i) {intv =g[u][i].v; if(Dist[v] > Dist[u] +g[u][i].dist) {Tmp.dist= Dist[v] = dist[u]+g[u][i].dist; TMP.V=v; Q.push (TMP); } } } returndist[y];}intSPFA (intXintYintN) { for(inti =1; I <= N; ++i) dist[i]=INF; Queue<int>Q; Q.push (x); DIST[X]=0; while(!Q.empty ()) { intU =Q.front (); Q.pop (); Vis[u]=false; for(inti =0; I < g[u].size (); ++i) {intv =g[u][i].v; if(Dist[v] > Dist[u] +g[u][i].dist) {Dist[v]= Dist[u] + g[u][i].dist;//Updates are available for updates if(!vis[v])//If the knot is inside the team, you don't have to repeat the queue.{Q.push (v); VIS[V]=true; } } } } returndist[y];}intMain () {intN, D, t; scanf ("%d", &t); for(intK =1; K <= T; ++k) {scanf ("%d%d", &n, &d); for(inti =1; I <= N; ++i) g[i].clear (); for(inti =1; I <= N; ++i) {scanf ("%d", &a[i].height); A[i].id=i; if(I! =1) G[i].push_back (Edge (i-1, -1)); } sort (A+1, A + n +1); BOOLFlag =true; for(inti =2; I <= N; ++i) {if(ABS (A[i].id-a[i-1].id) >d) {flag=false; Break; } g[min (A[i].id, A[i-1].id)].push_back (Edge (Max (a[i].id, A[i-1].id), d)); } printf ("Case %d:", K); if(!flag) puts ("-1"); Else { if(a[1].id >a[n].id) Swap (a[1].id, a[n].id); printf ("%d\n", SPFA (a[1].id, a[n].id,n)); } } return 0;}
The difference between the other point and the source point is the largest when the solution is solved by the shortest circuit.
When the solution is solved by the longest road, the difference between the other point and the source point is minimized.
2010 Multi-School first problem hdu3440house man differential constraint system