Surface
Test instructions: There are 2 people, all go to the subway, but they have a difference of x minutes, but they have been in the communication, so you know, you in the middle of the AB site, he is in the middle of the CD, (b must be equal to a+1 or a, the same as D), ask you every 2 stations between the time required to meet the Cannot satisfy the output impossible. (Platform from 1 to n,n<=2000)
Puzzle: Typical differential constraints we recall that for inequalities a-b<=x we build (a,b,x);
So for the subject
if (a==b && c==d) {build (d,a,-x); build (b,c,x);}
if (a==b && c!=d) {build (d,a,-x-1); build (b,c,x-1);}
if (a!=b) {build (d,a,-x-1); Build (B,c,x-1);}
In order to ensure the connectivity of the graph, remember to add a super source point, add 1 sides to each point
And the station, the actual problem, each adjacent to 2 stations, at least 1 minutes.
By the way the DFS version of the SPFA board, run fast
1#include <bits/stdc++.h>2 #defineN 20053 #defineM 100054 Const Long Longinf=0x3f3f3f3f3f3f3f3f;5typedefLong LongLLD;6 using namespacestd;7 structRec8 {9 intGo,next;Ten Long Longv; One }eg[m]; A intHead[n],que[n],nn,k,a,b,c,p,n,du[n]; - LLD Dis[n]; - BOOLUsed[n]; the voidReadint&a) - { -A=0; - CharC=GetChar (); + while(c<= +) c=GetChar (); - while(c> +) a=a*Ten+c-'0', c=GetChar (); + } A voidBuildintAintBLong Longc) at { -p++; -eg[p].go=b; -eg[p].next=Head[a]; -eg[p].v=C; -head[a]=p; in } -stack<int>S; to intSPFA () + { - for(intI=0; i<=n;i++) the { * S.push (i); $du[i]=1;Panax Notoginsengused[i]=1; -dis[i]=0; the } + while(!s.empty ()) A { the intv=s.top (); + S.pop (); -used[v]=0; $ for(intu=head[v];u;u=eg[u].next) $ if(dis[eg[u].go]>dis[v]+eg[u].v) - { -dis[eg[u].go]=dis[v]+eg[u].v; the if((++du[eg[u].go]) >nn) - {Wuyiprintf"Impossible"); the return 0 ; - } Wu if(!Used[eg[u].go]) S.push (eg[u].go); - } About } $ return 1 ; - } - intT,x,d,tt; - intMain () A { + inttt=0; thescanf"%d",&T); - while(t--) $ { thep=0; thememset (que,0,sizeof(que)); thememset (Used,0,sizeof(used)); theMemset (Du,0,sizeof(du)); -Memset (Head,0,sizeof(head)); in while(!S.empty ()) S.pop (); the Read (nn); Read ( k); Read (x); the for(intI=1; i<=k;i++) About { the Read ( a); Read (b); Read (c); Read (d) ; the if(A==b && c==d) the { +Build (d,a,-x); - build (b,c,x); the}ElseBayi if(A==b && c!=d) the { theBuild (d,a,-x-1); -Build (b,c,x-1); -}Else the { theBuild (d,a,-x-1); theBuild (b,c,x-1); the } - } then=nn+1; the for(intI=1; i<=nn;i++) Build (N,i,0); the for(intI=1; i<nn;i++) Build (i+1, i,-1);94tt++; theprintf"Case #%d:", TT); the intwhy=SPFA (); the if(why) for(intI=2; i<=nn;i++) printf ("%lld", dis[i]-dis[i-1]);98printf"\ n"); About } - return 0;101}
2017ccpc-final J Hdu-6252 Subway chasing differential constraint