洛谷 P3133 [USACO16JAN]无线电联系Radio Contact DP

题目描述

Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan out and search the farm along different paths, but stay in contact via radio so they can keep in touch with each-other. Unfortunately, the batteries in their radios are running low, so they want to plan their movements so as to conserve power, by trying to stay always within a short distance apart.

Farmer John starts at location (f_x,f_y) and plans to follow a path consisting of N steps, each of which is either ‘N’ (north), ‘E’ (east), ‘S’ (south), or ‘W’ west. Bessie starts at location (b_x, b_y) and follows a similar path consisting of M steps. Both paths may share points in common. At each time step, Farmer John can either stay put at his current location, or take one step forward along his path, in whichever direction happens to be next (assuming he has not yet reached the final location in his path). Bessie can make a similar choice. At each time step (excluding the first step where they start at their initial locations), their radios consume energy equal to the square of the distance between them.

Please help FJ and Bessie plan a joint movement strategy that will minimize the total amount of energy consumed up to and including the final step where both of them first reach the final locations on their respective paths.

FJ失去了他最喜欢的牛铃,而Bessie已经同意帮助他找到它!他们用不同的路径搜索农场,通过无线电保持联系。不幸的是,无线电中的电池电量不足,所以他们设法尽可能保持两者位置的距离最小,以节省电量。

FJ从位置(fx,fy)开始,并计划遵循由N步骤组成的路径,每个步骤都是“N”(北),“E”(东),“S”(南),或“W”(西)。Bessie从位置(bx,by)开始,并遵循由M步骤组成的类似路径。两个路径可以经过相同的点。在每个时间段,FJ可以保持在他现在的位置,或沿着他的道路前进一步,无论哪个方向恰好在下一个(假设他还没有到达他的路径的最后位置)。Bessie可以做出类似的选择。在每个时间步(不包括从初始位置开始的第一步),他们的无线电消耗的能量等于它们之间距离的平方。

请帮助FJ和Bessie计划行动策略,最大限度地减少消耗的能量总量。总量包括最终步骤,这时两者首先到达各自路径上的最终位置。

输入格式

The first line of input contains N and M (1000≤N,M≤1000). The

second line contains integers f_x and f_y, and the third line contains b_x and b_y. The next line contains a

string of length NN describing FJ’s path, and the final line contains a string

of length MM describing Bessie’s path.

It is guranteed that Farmer John and Bessie’s coordinates are always in the

range (0 \leq x,y \leq 10000≤x,y≤1000) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.

第一行输入N和M(1≤N,M≤1000)。

第二行输入整数fx和fy,第三行输入bx和by(0≤fx,fy,bx,≤1000)。下一行包含一个长度为N的字符串描述FJ的路径,最后一行包含一个字符串的长度M描述Bessie的路径。

数据满足(0≤x,y≤1000)。注意,东方向为正X方向,北方向为正Y方向。

输出格式
Output a single integer specifying the minimum energy FJ and Bessie can use

during their travels.

输出一个整数,表示最小能量。

输入输出样例

输入 #1

2 7

3 0

5 0

NN

NWWWWWN

输出 #1

28

说明/提示

感谢@ prcups 改进翻译

解法:动态规划

  • 我们预处理出他们两个人所有步数走过时的坐标,然后设一个数组f[i][j]记录第一个人走i步,第二个人走j步之后的最小值

  • 我们很容易得到转移方程f[i][j]=min(f[i-1][j],f[i][j-1],f[i-1][j-1])+sum(i,j)当前的花费

AC代码

#include<cstdio>
#include<cstring>
#define re register int
using namespace std;
int n,m,f[1010][1010],xx[211],yy[211];
int fx[1010],fy[1010],bx[1010],by[1010];
char fj[1010],bs[1010];
inline int sum(int x,int y) {
	return (fx[x]-bx[y])*(fx[x]-bx[y])+(fy[x]-by[y])*(fy[x]-by[y]);
}
inline int min(int A,int B) { return A<B?A:B; } 
int main() {
	scanf("%d%d%d%d",&n,&m,&fx[0],&fy[0]);
	scanf("%d%d%s%s",&bx[0],&by[0],fj+1,bs+1);
	yy['N']=1,xx['E']=1,yy['S']=-1,xx['W']=-1;
	for(re i=1;i<=n;i++) {
		fx[i]=fx[i-1]+xx[fj[i]];
		fy[i]=fy[i-1]+yy[fj[i]];
	}
	for(re i=1;i<=m;i++) {
		bx[i]=bx[i-1]+xx[bs[i]];
		by[i]=by[i-1]+yy[bs[i]];
	}
	memset(f,0x3f,sizeof(f)); f[0][0]=0;
	for(re i=1;i<=n;i++) {
		f[i][0]=sum(i,0)+f[i-1][0];
	}
	for(re i=1;i<=m;i++) {
		f[0][i]=sum(0,i)+f[0][i-1];
	}
	for(re i=1;i<=n;i++) {
		for(re j=1;j<=m;j++) {
			f[i][j]=min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))+sum(i,j);
		}
	}
	printf("%d",f[n][m]);
	return 0; 
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值