Referred cola 1 , Hamburg, -1 , i.e. the process of seeking an absolute value does not exceed k Shortest Path.
Then find k range is only 10 , that is the only course of legal values 21 species, and therefore run again dij or SPFA (hey hey hey) can be.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pi pair<int,int> 4 #define mp make_pair 5 #define fi first 6 #define se second 7 #define N 10005 8 queue<pi >q; 9 struct ji{ 10 int nex,to,len; 11 }edge[N*20]; 12 int E,t,n,m,k,x,y,z,ans,d[N][41],vis[N][41],head[N],p[N]; 13 void add(int x,int y,int z){ 14 edge[E].nex=head[x]; 15 edge[E].to=y; 16 edge[E].len=z; 17 head[x]=E++; 18 } 19 int main(){ 20 scanf("%d",&t); 21 while (t--){ 22 scanf("%d%d%d",&n,&m,&k); 23 memset(d,0x3f,sizeof(d)); 24 memset(head,-1,sizeof(head)); 25 memset(vis,0,sizeof(vis)); 26 E=0; 27 for(int i=1;i<=n;i++){ 28 scanf("%d",&p[i]); 29 p[i]=p[i]*2-3; 30 } 31 for(int i=1;i<=m;i++){ 32 scanf("%d%d%d",&x,&y,&z); 33 add(x,y,z); 34 add(y,x,z); 35 } 36 scanf("%d%d",&x,&y); 37 d[x][p[x]+20]=0; 38 vis[x][p[x]+20]=1; 39 q.push(mp(x,p[x]+20)); 40 while (!q.empty()){ 41 pi o=q.front(); 42 q.pop (); 43 VIS [o.fi] [o.se] = 0 ? 44 for ( int i = head [o.fi]? I! = - 1 ? I = EDGE [i] .nex) { 45 x = EDGE [i] .TO? 46 the if ((abs (o.se + p [x] - 20 ) <= k) && (d [o.fi] [o.se] + EDGE [i] .len <d [x] [o.se + p [x]])) { 47 d [x] [o.se + p [x]] = d [o.fi] [o.se] + EDGE [i] .len? 48 the if (! VIS [x] [o.se + p [x]]) { 49 q.push (mp (x, o.se + p [x]))? 50 VIS [x] [o.se + p [x]] = 1; 51 } 52 } 53 } 54 } 55 years = 0x3f3f3f3f ; 56 for ( int i = 20 k; i <= k + 20 ; i ++) ans = min (years d [y] [i]); 57 if (age == 0x3f3f3f3f ) ans = - 1 ; 58 printf ( " % d \ n " , year); 59 } 60 }