Title Description
In the graph G, each edge has a length of 1 and is given a starting and ending point, so you can find a path from the starting point to the end point in the diagram that meets the following criteria:
1. The point at which all points on the path point is connected directly or indirectly to the endpoint.
2. The shortest path is satisfied if condition 1 is met.
Note: There may be heavy and self-loops in Figure g, and the topic guarantees that the end point does not have an edge.
Please output the length of the path that matches your criteria.
Input/output format
Input format:
The input file name is road. In.
The first line has two integers of N and M separated by a space, indicating that the graph has n points and m edges.
The next M-line is 2 integers x, y, and separated by a space, indicating that an edge is pointing from point X to point Y.
The last line has two integers separated by a space s, T, which indicates the starting point is S and the end point is T.
Output format:
The output file is named Road. Out.
The output has only one row and contains an integer representing the length of the shortest path that satisfies the topic. If such a path does not exist, output-1.
Input/Output sample
Input Sample # #:
3 2 1 2 2 1 1 3
Sample # # of output:
-1
Input Sample #:
6 6 1 2 1 3 2 6 2 5 4 5 3 4 1 5
Output Example #:
3
Description
Explanation 1:
As shown, the arrows indicate a direction to the road, and the dots represent the city. Start 1 and end 3 are not connected, so the problem is satisfied
The path does not exist, so output-1.
Explanation 2:
As shown, the path that satisfies the condition is 1->3->4->5. Note that point 2 cannot be in the answer path because point 2 is connected to a side to 6, and point 6 is not connected to the end 5.
For 30% of data,0<n≤10,0<m≤20;
For 60% of data,0<n≤100,0<m≤2000;
For 100% of data, 0<n≤10,000,0<m≤200,000,0<x,y,s,t≤n,x≠t.
Thinking of solving problems
The use of wide search first remove all the points that can not be used, the specific is to run to the assignment, in the loop to find the run, and he connected to the points are removed, just need to remove directly connected on the line!!! Then run side Dij or SPFA will be able to live, here to pay attention to the point and edge are a bit more, should be stored in the list
ProgramChoosepath;varpd:array[0..10000] ofBoolean; U,v,head,next:Array[0..200000] ofLongint; B,d,c:Array[0..10000] ofLongint; I,j,m,n,now,x,y,h,t,l,sum:longint;procedureDij;varI,min,minn,l:longint;begin fori:=1 toN Dod[i]:=Maxlongint; D[y]:=0; fori:=1 toN Do beginmin:=Maxlongint; forj:=1 toN Do if(Min>d[j]) and(Pd[j]) Then beginmin:=D[j]; Minn:=J; End; ifMin=maxlongint Thenexit; Pd[minn]:=false; L:=Head[minn]; whileL<>0 Do begin if(min+1<D[V[L]]) and(Pd[v[l]]) Thend[v[l]]:=min+1; L:=Next[l]; End; End;End;beginread (n,m); fori:=1 toM Do beginread (v[i],u[i]); Next[i]:=Head[u[i]]; Head[u[i]]:=i; End; Read (x, y); H:=0; T:=0; Inc (T); B[T]:=y; whileH<=t Do beginInc (H); Now:=B[h]; Pd[now]:=true; L:=Head[now]; whileL<>0 Do begin ifPd[v[l]]=false Then beginInc (T); B[T]:=V[l]; PD[V[L]]:=true; End; L:=Next[l]End; End; fori:=1 toN Do ifPd[i]=false Then beginInc (SUM); C[sum]:=i; End; fori:=1 toSum Do beginL:=Head[c[i]]; whileL<>0 Do beginPd[v[l]]:=false; L:=Next[l]; End; End; Dij; Writeln (D[x]);End.
Rocky Valley 2296 Finding the way