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 and Output Sample input example # #:
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.
Ideas:
When the edge is saved
Reverse edge to Dfs to find the point where the endpoint can reach (point to the end)
Then mark the point at which the point of the edge can reach the end to true.
And then once again build the SPFA on the point of true
Output shortest circuit
Easy AC
Come on, on the code:
#include <queue>#include<cstdio>#defineINF 0X7FFFFFFusing namespacestd;structNode {int from, To,dis,next;};structNode edge[200001],edge_[200001];intn,m,head_[10001],head[10001],s,t,num=0, dis[10001];BOOLif_[10001],can[10001];inlinevoidEdge_add (int from,intTo,intDis_) {num++; Edge[num].to=to ; Edge[num].dis=Dis_; Edge[num]. from= from; Edge[num].next=head[ from]; head[ from]=num; Edge_[num]. from=to ; Edge_[num].to= from; Edge_[num].dis=Dis_; Edge_[num].next=Head_[to]; Head_[to]=num;}voidSearchintNow ) {If_[now]=true; for(intI=head_[now];i;i=Edge_[i].next) { if(!if_[edge_[i].to]) search (edge_[i].to); }}voidSPFA () {if(!can[s])return; Queue<int>que; Que.push (s); while(!Que.empty ()) { for(intI=head[que.front ()];i;i=Edge[i].next) { if(Can[edge[i].to]) {if(Edge[i].dis+dis[que.front ()]<Dis[edge[i].to]) {Dis[edge[i].to]=edge[i].dis+Dis[que.front ()]; if(!If_[edge[i].to]) {If_[edge[i].to]=true; Que.push (edge[i].to); }}}} If_[que.front ()]=false; Que.pop (); }}intMain () {scanf ("%d%d",&n,&m); int from, to; for(intI=1; i<=m;i++) {scanf ("%d%d",& from,&to ); Edge_add ( from, To,1); } scanf ("%d%d",&s,&t); Search (t); BOOLIf_break; for(intI=1; i<=n;i++) {If_break=false; if(!if_[i])Continue; for(intj=head[i];j;j=Edge[j].next) { if(!If_[edge[j].to]) {If_break=true; Break; } } if(If_break)Continue; Can[i]=true; } for(intI=1; i<=n;i++) dis[i]=inf,if_[i]=false; Dis[s]=0, if_[s]=true; SPFA (); if(dis[t]>=200005) printf ("-1\n"); Elseprintf"%d\n", dis[t]); return 0;}
AC Diary-looking for the road Valley P2296