码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 238E. Meeting Her 图论+记忆化搜索

时间:2015-07-05 23:59:07      阅读:463      评论:0      收藏:0      [点我收藏+]

标签:


大意:

有一个 n 个结点的有向图,边权均为 1。Urapl 想从 a 出发去 b。有 p 个公交车公司。在每
一秒的开始,第 i 个公司的公交车随机选择一条从 s i 到 t i 的最短路径然后走这条路径。如果
一个公交车经过 Urpal 所在的交叉点,则 Urpal 可以上这辆公交车,他可以在中途任意一个结
点下车。
在任何时刻 Urpal 只知道他自己的位置和约会地点。当他上了公交车时他只知道这辆公交
车属于第几个公司。当然 Urpal 知道城市地图和每个公司的 (s i , t i )。
求最坏情况下他需要乘坐公交车的次数。


解法:

先求出每辆公交车必须经过的点,再对必须经过的点搜索,算出每个点到目的地最坏情况下还要转几次车

E. Meeting Her
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There arek bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will 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 time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.

At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si,?ti) for each company.

Note that Urpal doesn‘t know buses velocity.

Input

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

The next m lines contain two integers each ui and vi (1?≤?ui,?vi?≤?nui?≠?vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.

The next line contains an integer k (0?≤?k?≤?100). There will be k lines after this, each containing two integers si and ti(1?≤?si,?ti?≤?nsi?≠?ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.

Output

In the only line of output print the minimum number of buses Urpal should get on on his way 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 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
output
2
input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
output
-1


/* ***********************************************
Author        :CKboss
Created Time  :2015年07月05日 星期日 22时53分11秒
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,&m,&a,&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);
		dist[u][v]=1;
	}

	/// floyd
	for(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 incur
	scanf("%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 i
				bool 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;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 238E. Meeting Her 图论+记忆化搜索

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/46767327

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!