noip2014 improvement of group Day2 two problems-RLQ

Source: Internet
Author: User

( again yesterday's homework ...) Ben inscribed on yesterday)

(This essay has been doing so long, I am not eating jujube pills ... )

(Okay, so this is a graph topic?)

Address: http://www.luogu.org/problem/show?pid=2296

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.

------------------------------your friend split line bacteria are logged in------------------------------------------------------------------------------ ----------------------

Test instructions to understand.

First you have to get rid of the points you can't go to, DFS is fine.

Probably the input time to save a counter-image, and then the inverse of the image from the end of the point can not be marked, and then the points in the image of the edge attached to the point in the positive image is removed (just remove the direct connection point of the HA)

Then the shortest circuit (SPFA really faster than DIJ)

I really want to finish the flood problem.

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4typedefstruct{5         intto ;6         intNext;7 }line;8Line gra[200001],shg[200001];9 inthead[10001]={0},shh[10001]={0},num=0, note[10001]={0};Ten intN,m;/*n points, M edge*/ One ints,t; A intdis[10001],que[10001]={0},pos[10001]={0}; - intAddint  from,intTo ) { -num++; thegra[num].next=head[ from]; -gra[num].to=to ; -head[ from]=num; -shg[num].next=Shh[to]; +shg[num].to= from; -shh[to]=num; +     return 0; A } at intPointintgoal) { -     intL=Shh[goal]; -      while(l!=0){ -           if(note[shg[l].to]==0){ -note[shg[l].to]=1; - Point (shg[l].to); in           } -L=Shg[l].next; to     } +     return 0; - } the intshut () { *     intI=1; $      while(i<=N) {Panax Notoginseng           if(note[i]==0&&i!=t) { -              intL=Shh[i]; the               while(l!=0){ +head[shg[l].to]=0; AL=Shg[l].next; the              } +           } -i++; $     } $     return 0; - } - intSPFA () { the      for(intI=1; i<=n;i++) dis[i]=10000000; -     intL,he=0, tail=1;Wuyidis[s]=0; theque[he]=s; -pos[s]=1; Wu      Do{ -L=Head[que[he]]; About         while(l!=0){ $              if(dis[gra[l].to]>=dis[que[he]]+1){ -dis[gra[l].to]=dis[que[he]]+1; -                 if(pos[gra[l].to]==0){ -tail++; Aque[tail]=gra[l].to; +pos[gra[l].to]=1; the                 } -              } $L=Gra[l].next; the        } thehe++; the} while(he<=tail); the     if(dis[t]==10000000)return-1; -     Else returnDis[t]; in } the intMain () { thescanf"%d%d",&n,&m); About      for(intI=1; i<=m;i++){ thescanf"%d%d",&s,&t); the Add (s,t); the     } +scanf"%d%d",&s,&t); - Point (t); the shut ();Bayi     if(note[s]==0){ theprintf"-1"); the        return 0; -     } -     //for (int i=1;i<=n;i++) printf ("%d", Note[i]); theprintf"%d", SPFA ()); the     return 0; the}

noip2014 improvement of group Day2 two problems-RLQ

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.