地铁网络(分层图-哈密尔顿路径)

地铁网络(分层图-哈密尔顿路径)_第1张图片

地铁网络(分层图-哈密尔顿路径)_第2张图片

最近Csdn的图片似乎有了改革(1.相册见不到了?搬家难度++ 2.水印   3.图片地址长度++  4.似乎可以用来图床了?TNY)

言归正传:这题是分层图(废话)的加强版——分层图+哈密尔顿路径!!!(几乎没变求D……)

话说我根本不会哈密尔顿啊(求D),指数级算法a(求D)

接下来讲讲第一次写哈密尔顿(都不屑D我)的Exp:

1.如果你想一个状态由它的真子集递归而来 那么(从小到大枚举) Rea:A的真子集<A

2.终点起点一定要拆(以防S,T点拐弯)

3.由2可知初始化和求答案都要考虑2

4.除非k=0,否则S,T点的换乘一定为INF时间


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (50000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
ll a,b;
int n;
char s[1000];
int x[1000],y[1000];
int main()
{
//	freopen("robot.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>a>>b;
	scanf("%s",s+1);n=strlen(s+1);
	x[0]=y[0]=0;
	bool bo=0;
	For(i,n)
	{
		if (s[i]=='R') x[i]=x[i-1]+1,y[i]=y[i-1];
		if (s[i]=='L') x[i]=x[i-1]-1,y[i]=y[i-1];
		if (s[i]=='U') x[i]=x[i-1],y[i]=y[i-1]+1;
		if (s[i]=='D') x[i]=x[i-1],y[i]=y[i-1]-1;
		if (x[i]==a&&y[i]==b) bo=1;
	}
	if (bo==1)
	{
		cout<<"Yes"<<endl;
		return 0;
	}
	Rep(i,n+1)
	{
		int t1=a-x[i],t2=b-y[i];
		if (t1!=0&&x[n]==0) continue;
		if (t2!=0&&y[n]==0) continue;
		if (x[n]==0||y[n]==0)
		{
			bool bo1=0,bo2=0;
			if (x[n]==0) bo1=1;
			if (y[n]==0) bo2=1;
			if (bo1&&!bo2) if (t2%y[n]==0&&t2/y[n]>=0) {cout<<"Yes"<<endl;return 0;} else continue; 
			if (!bo1&&bo2) if (t1%x[n]==0&&t1/x[n]>=0) {cout<<"Yes"<<endl;return 0;} else continue;
			if (!x[i]&&!y[i]) {cout<<"Yes"<<endl;return 0;} else continue;
		}
		if (t1%x[n]||t2%y[n]) continue;
		t1/=x[n],t2/=y[n];
		if (t1<0||t2<0||t1^t2) continue;
		cout<<"Yes"<<endl;
		return 0;
	}
	cout<<"No"<<endl;
	
	return 0;
}




你可能感兴趣的:(地铁网络(分层图-哈密尔顿路径))