BZOJ 4510: [Usaco2016 Jan]Radio Contact dp

来源:互联网 发布:榕基软件千股千评 编辑:程序博客网 时间:2024/06/06 00:16

4510: [Usaco2016 Jan]Radio Contact

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 76  Solved: 61
[Submit][Status][Discuss]

Description

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 (fx,fy) 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 (bx,bybx,by) and follows a similar path consisting of MM 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.

Input

The first line of input contains N and M (1< =N,M< =1000). The second line contains integers fx and fy, and the third line contains bxbx and byby (0< =fx,fy,bx,by< =10000). The next line contains a string of length N 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< =x,y< =1000) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.

Output

Output a single integer specifying the minimum energy FJ and Bessie can use during their travels.

Sample Input

2 7
3 0
5 0
NN
NWWWWWN

Sample Output

28

随便dp.


#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline int read(){    int x=0,c=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0',c=getchar();}    return x*f;}void print(int x){if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}const int N=1010;const int XX[4]={0,0,-1,1},YY[4]={1,-1,0,0};struct point{int x,y;friend ll dis(const point &x,const point &y){return 1ll*(x.x-y.x)*(x.x-y.x)+1ll*(x.y-y.y)*(x.y-y.y);}}p[2][N];char path[2][N];ll f[N][N];int main(){int n=read(),m=read();register int i,j;p[0][0].x=read();p[0][0].y=read();p[1][0].x=read();p[1][0].y=read();scanf("%s",path[0]+1);scanf("%s",path[1]+1);for(i=1;i<=n;++i)switch(path[0][i]){case 'N':p[0][i].x=p[0][i-1].x+XX[0];p[0][i].y=p[0][i-1].y+YY[0];break;case 'S':p[0][i].x=p[0][i-1].x+XX[1];p[0][i].y=p[0][i-1].y+YY[1];break;case 'W':p[0][i].x=p[0][i-1].x+XX[2];p[0][i].y=p[0][i-1].y+YY[2];break;default :p[0][i].x=p[0][i-1].x+XX[3];p[0][i].y=p[0][i-1].y+YY[3];break;}for(i=1;i<=m;++i)switch(path[1][i]){case 'N':p[1][i].x=p[1][i-1].x+XX[0];p[1][i].y=p[1][i-1].y+YY[0];break;case 'S':p[1][i].x=p[1][i-1].x+XX[1];p[1][i].y=p[1][i-1].y+YY[1];break;case 'W':p[1][i].x=p[1][i-1].x+XX[2];p[1][i].y=p[1][i-1].y+YY[2];break;default :p[1][i].x=p[1][i-1].x+XX[3];p[1][i].y=p[1][i-1].y+YY[3];break;}memset(f,0X3f,sizeof(f));f[0][0]=0;for(i=0;i<=n;++i)for(j=0;j<=m;++j){f[i+1][j]=min(f[i+1][j],f[i][j]+dis(p[0][i+1],p[1][j]));f[i][j+1]=min(f[i][j+1],f[i][j]+dis(p[0][i],p[1][j+1]));f[i+1][j+1]=min(f[i+1][j+1],f[i][j]+dis(p[0][i+1],p[1][j+1]));}cout<<f[n][m]<<endl;return 0;}