Codeforces 238E. Meeting She graph theory + memory Search

Source: Internet
Author: User


Effect:

There is a graph of n nodes, Benquan are 1. URAPL wants to go from a to B. There is a bus company of P. In every
At the beginning of a second, the bus of the I company randomly selects a shortest path from S I to t I and then goes this path. If
A bus through the intersection of Urpal, then Urpal can on the bus, he can be in the middle of any one knot
Point to get off.
At any moment Urpal only knows his own location and place of appointment. When he got on the bus, he only knew about the bus.
The car belonged to the first few companies. Of course Urpal knows the city map and each company's (s I, t i).
The number of times he needs to take a bus in the worst case scenario.


Solution:

First find out the points that each bus must pass, and then search for the points that must pass, and calculate the worst case for each point to the destination.

E. Meeting Hertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Urpal lives in a big city. He has planned to meet He lover tonight.

The city hasNJunctions numbered from1ToN. The junctions is connected bymDirected streets, all the roads has equal length. Urpal lives in JunctionaAnd the date is planned in a restaurant in junctionb. He wants to use public transportation to get to Junctionb. There iskBus transportation companies. At the beginning of every second, a bus from theI-th Company chooses a random shortest path between junction si and junction Ti and passes through it. There might is no path from si To Ti . In so case no bus would leave from si To Ti . If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.

Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the TI Me of travel are the sum of length of the traveled roads) and what's the minimum number of buses he should take in the WOR St case.

< Span class= "TEX-FONT-STYLE-BF" style= "Font-weight:bold" >at any moment Urpal knows only he own position and the place wher E the date would be. When he gets on the bus he knows only the index of the that company's this bus. Of course Urpal knows the city map and the the The Pairs  ( s i ,? t i )  for each company.

Note that Urpal doesn ' t know buses velocity.

Input

The First line of the input contains four integers  n ,   m ,   a ,   b   (2?≤? n ? ≤?100; 0?≤? m ? ≤? N · ( n ?-? 1);  1?≤? a ,? b ? ≤? n ;   a ? ≠? b ) .

The nextmlines contain, integers each ui and vi (1?≤? ) Ui,? v i? ≤? n; u i? ≠? v i)Describing a directed road from junction ui To Junction vi . All roads in the input would be distinct.

The next line contains an integerk (0?≤? ) K. ≤?100). There'll bekLines after this, each containing the integers si and Ti (1?≤? ) si,? T i? ≤? n; s i? ≠? T i)Saying there is a bus route starting at si and ending at Ti . Please note that there might is no path from si To Ti , this was described in the problem statement.

Output

In the-line of output print the minimum number of buses urpal should get on the He-in-the-worst case. If it ' s not possible to reach the destination in the worst case print-1.

Sample Test (s) input
7 8 1 71 21 32 43 44 64 56 75 732 71 45 7
Output
2
Input
4 4 1 21 21 32 43 411 4
Output
-1


/* ***********************************************author:ckbosscreated time:2015 July 05 Sunday 22:53 11 seconds file Name : cf238e_2.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int Maxn=110;const int inf=0x3f3f3f3f;int n,m,a,b,t;int from[maxn],to[maxn];int dist[maxn][maxn];int INCUR[MAXN][MAXN ];bool vis[maxn];int g[maxn],f[maxn];int dfs (int cur,int aim) {if (Cur==aim) return f[cur];if (vis[cur]==true) return g[ Cur];vis[cur]=true; g[cur]=0;for (int i=1;i<=n;i++) {if (Dist[cur][i]+dist[i][aim]==dist[cur][aim]&&dist[cur][aim]==dist[i] [Aim]+1] G[cur]=max (G[cur],dfs (I,aim));} Return G[cur]=min (G[cur],f[cur]);} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout); scanf ("%d%d%d%d",&N,&AMP;M,&AMP;A,&AMP;B); memset (dist,63,sizeof (Dist)); for (int i=1;i<=n;i++) dist[i][i]=0;for (int i=0,u,v;i<m ; i++) {scanf ("%d%d", &u,&v);d ist[u][v]=1;} floydfor (int k=1;k<=n;k++) for (int. i=1;i<=n;i++) for (int j=1;j<=n;j++) dist[i][j]=min (Dist[i][j],dist[i] [K]+dist[k][j]);///Get INCURSCANF ("%d", &t), for (int k=0;k<t;k++) {scanf ("%d%d", from+k,to+k); int s=from[k],t= To[k];if (Dist[s][t]==inf) continue;for (int i=1;i<=n;i++) {if (Dist[s][i]+dist[i][t]==dist[s][t]) {//Check Ibool flag=true;for (int j=1;j<=n&&flag;j++) {if (j==i) continue;if (Dist[s][j]+dist[j][t]==dist[s][t]) {if (dist S [J]==dist[s][i]) Flag=false;}} if (flag==true) incur[k][i]=1;}}} memset (F,63,sizeof (f)); F[b]=0;while (TRUE) {bool Gono=false;for (int i=0;i<t;i++) {if (Dist[from[i]][to[i]]==inf) Continue;memset (vis,0, sizeof (VIS)); for (int j=1;j<=n;j++) {if (incur[i][j]) {int Temp=dfs (j,to[i]) +1;if (Temp<f[j]) {f[j]=temp; gono= true;}}}} if (gono==false) break;} int ans=f[a];if (Ans>=inf) ans=-1;printf ("%d\n", ans); return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 238E. Meeting She graph theory + memory Search

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.