Describe
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:
- The point at which all points on the path point is connected directly or indirectly to the endpoint.
- 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.
Format input Format
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 has only one row and contains an integer that represents the length of the shortest path that satisfies the topic description.
If such a path does not exist, output-1.
Example 1 sample input 1[copy]
3 21 22) 11 3
Sample output 1[Copy]
-1
Example 2 sample input 2[copy]
6 61 21 32 62 54 53 41 5
Sample output 2[Copy]
3
Limit
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.
Tips
"Input and Output Example 1 description"
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 path to meet the problem description does not exist, so output-1.
"Input and Output Example 2 description"
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.
Source
NOIP2014 Raising Group Day2
Analysis
In the beginning, I was thinking of using Dfs to find the points that meet the requirements, and then ask for a SSSP. However, it was found that it was difficult to deal with the ring case. Later saw some of the problems of God Ben (they have no exception to write "This is a water problem"), found that the first time DFS to find a node can be the same as the endpoint, and then according to test instructions to delete those non-compliant points (I just because I did not clear the topic which points to be removed, debugging the afternoon) , the shortest path can be found at the end of the BFS. As far as there is no road, as long as BFS plus a judgment on it.
In addition to this hundreds of thousands of level of data under the people still less use iostream better, often easy to be pits ...
Code
1#include <cstdio>2#include <vector>3#include <queue>4 using namespacestd;5vector<int> u[10001];6vector<int> v[10001];7vector<int>F;8 BOOLb[10001];9 BOOLi[10001];Ten intm, n, S, T, ans; One voidDfsinti) A { -B[i] =true; - for(intj =0; J! = V[i].size (); ++j) the if(!B[v[i][j]]) - DFS (V[i][j]); - } - voidCleaninti) + { - if(!B[i]) { + for(intj =0; J! = V[i].size (); ++j) { A if(B[v[i][j]]) { at F.push_back (V[i][j]); - } - } - } - } - voidBFS () in { -queue<int>Q; toqueue<int>L; + Q.push (s); -L.push (0); theI[s] =true; * BOOLFlag =true; $ while(! Q.empty () &&flag) {Panax Notoginseng intK =Q.front (); - intL =L.front (); the Q.pop (); + L.pop (); A if(k = =t) { theAns =l; +Flag =false; - } $ for(inti =0; I! = u[k].size () && flag; ++i) { $ if(B[u[k][i]) && (!I[u[k][i])) { -I[u[k][i]] =true; - Q.push (U[k][i]); theL.push (L +1); - }Wuyi } the } - if(flag) WuAns =-1; - } About intMain () $ { -scanf"%d%d", &n, &m); - for(inti =0, u, v; I! = m; ++i) { -scanf"%d%d", &u, &v); A U[u].push_back (v); + v[v].push_back (u); the } -scanf"%d%d", &s, &t); $ DFS (t); the if(!B[s]) theAns =-1; the if(ans = =0) { the for(inti =1; I <= N; ++i) - Clean (i); in for(inti =0; I! = F.size (); ++i) theB[f[i]] =false; theB[s] =true; About BFS (); the } theprintf"%d", ans); the return 0; +}
[NOIP2014] Looking for the road