Test instructions
There are n people in the queue, according to the sequence numbered 1~n, now the distance between the two of them constrained, there are upper and lower limits, indicating dis[a,b]<=c or dis[a,b]>=c, ask the 1th person and the nth person the maximum distance possible? (If the INF is output-2, if the conflict is output-1, otherwise the output distance)
Ideas:
When the map is built, the constraint is turned into the standard form of a-b<=c, and then a b->a edge is built, and the right is C. Then the shortest way, note that the most short-circuit run out of the result is the furthest legal distance, rather than the shortest distance. The subject does not need to add auxiliary edge, as long as not reach N, the distance is INF, output-2, if there is a negative ring, it must be a conflict, is-1.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <Set>7#include <deque>8#include <map>9#include <algorithm>Ten#include <vector> One#include <iostream> A #definePII pair<int,int> - #defineBack Que[rear-1] - #defineINF 0x3f3f3f3f the #defineLL Long Long - #defineULL unsigned long Long - using namespacestd; - Const DoublePI = ACOs (-1.0); + Const intn=1010; - structnode + { A int from, To,dis,next; at node () {}; -Nodeint from,intTo,intDisintNext): from( from), to, dis (dis), next (next) {}; -}edge[n*N]; - - intedge_cnt, head[n]; - intInq[n], cnt[n], dist[n], N; in - voidAdd_node (int from,intTo,intdis) to { +Edge[edge_cnt]=node ( from, to,dis,head[ from]); -head[ from]=edge_cnt++; the } * $ intSPFA (intStinted)Panax Notoginseng { -memset (CNT,0,sizeof(CNT));//Number of Enqueue thememset (INQ,0,sizeof(INQ));//are you in the team +memset (Dist,0x3f,sizeof(Dist));//Distance Adeque<int> Que (1, ST); theinq[st]=1; +dist[st]=0; - while(!que.empty ()) $ { $ intt=Que.front (); Que.pop_front (); -inq[t]=0; node E; - for(intI=HEAD[T]; i!=-1; I=e.next) the { -E=Edge[i];Wuyi if(dist[e.to]>dist[t]+E.dis) the { -dist[e.to]=dist[t]+E.dis; Wu if(!inq[e.to])//not in the queue - { About if(++cnt[e.to]>n)//too many times in the queue $ return-1; -inq[e.to]=1;//The following is an optimization that can be deleted - if(!que.empty () && dist[e.to]<Dist[que.front ()]) - Que.push_front (e.to); A ElseQue.push_back (e.to); + } the } - $ } the } the returndist[ed]==inf?-2:d ist[ed]; the } the - voidInit () in { theEdge_cnt=0; the //for (int i=0; i<=n; i++) head[i]=-1; Aboutmemset (head,-1,sizeof(head)); the } the the intMain () + { -Freopen ("Input.txt","R", stdin); the intX, Y, A, B, C, t;cin>>T;Bayi while(t--) the { the init (); -scanf"%d%d%d",&n,&x,&y); - for(intI=1; i<=x; i++)//most the { thescanf"%d%d%d",&a,&b,&c); the Add_node (a,b,c); the } - for(intI=1; i<=y; i++)//Minimum the { thescanf"%d%d%d",&a,&b,&c); theAdd_node (b,a,-c);94 } theprintf"%d\n", SPFA (1, N)); the } the return 0;98}
AC Code
HDU 3592 World Exhibition (differential constraint, SPFA, water)