noip2014 looking for the road

Source: Internet
Author: User

P2296 looking for the road
      • 707 through
      • 2.6K Submit
    • Topic provider The user does not exist
    • Tag graph on noip Improvement Group
    • Difficulty popularization +/raise

Submit a discussion of the problem record

Recent discussions
    • Self-test AC, Evaluation machine re????????
    • 2-9mle,10re
    • Why is the 9th tle?
    • Re what ghost?
    • Mle
    • Data water
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.

Spit Groove: Put out the side to see leaks, and then has been WA, to tot forget the Qing 0,i=next[i] written i++, has been re ...

Analysis: First, how to know whether a point I is connected with the endpoint? Can be Floyd, but for this problem is not needed, from the end of the reverse one BFS, can be marked to the point is able to access, because the edge of 1, to find the shortest path from the beginning of the BFS once again. Before each expansion, we must first determine whether the edge is marked, After a BFS from the end point, be sure to clear the used array by 0.

#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =10010, MAXM =200010;intN, M,FIRST,TAIL,X[MAXM],Y[MAXM],NEXTT[MAXM *2],TO[MAXM *2],HEAD[MAXM],TOT,S,T,Q[MAXN],STEP[MAXN];BOOLVIS[MAXN];voidAddintAintb) {to[++tot] =b; Nextt[tot]=Head[a]; Head[a]=tot;}voidbfs1 () {q[0] =T; Vis[t]=true; First=0; Tail=1;  while(First <tail) {        intU = q[first++];  for(inti = Head[u];i;i =Nextt[i]) {            if(!Vis[to[i]]) {Vis[to[i]]=true; Q[tail++] =To[i]; }        }    }}BOOLChubian (intq) {     for(inti = head[q]; I i =Nextt[i])if(!Vis[to[i]])return false; return true;}BOOLbfs2 () { First=0; Tail=1; q[0] =s; Step[s]=0;  while(First <tail) {        intU = q[first++]; if(!Chubian (u))Continue;  for(inti = Head[u]; I i =Nextt[i]) {            if(Step[to[i]] = =-1) {Step[to[i]]= Step[u] +1; Q[tail++] =To[i]; if(To[i] = =t) {printf ("%d", Step[to[i]]); return true; }            }        }    }    return false;}intMain () {memset (Vis,false,sizeof(VIS)); scanf ("%d%d", &n, &m);  for(inti =1; I <= m; i++) {scanf ("%d%d", &x[i], &Y[i]); Add (Y[i], x[i]); //anti-Plus to facilitate the anti-BFS} scanf ("%d%d", &s,&t);    BFS1 (); memset (Head,0,sizeof(head)); memset (step,-1,sizeof(step)); memset (q,0,sizeof(q)); memset (Nextt,0,sizeof(NEXTT)); Memset (To,0,sizeof(to)); Tot=0;  for(inti =1; I <= m; i++) Add (X[i], y[i]); if(!Vis[s]) {printf ("-1\n"); return 0; }    if(!BFS2 ()) printf ("-1\n"); return 0;}

noip2014 looking for the road

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.