bzoj4510: [Usaco2016 Jan]Radio Contact
bzoj4510: [Usaco2016 Jan]Radio Contact
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 17
[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
3 0
5 0
NN
NWWWWWN
Sample Output
28
HINT
Source
题目大意:给出两个人的路径,在每个时刻可以选择任意一个人走或者两个人都走,在每个时刻的代价为两个人之间距离的平方。求两个人都走到终点时最小的代价。
题解:TAT现在只能写傻逼奶牛题了。。。设f[i][j]表示当前第一个人走了i步第二个人走了j步。。
f[i+1][j]=min(f[i+1][j],f[i][j]+dis())...f[i][j+1]和f[i+1][j+1]同理。。。
然后为啥这么慢啊TAT
#include<cstdio> #include<iostream> #include<cstring> #define rep(i,l,r) for(int i=l;i<=r;i++) #define N 1024 using namespace std; int n,m,a[N][2],b[N][2]; typedef long long ll; ll f[N][N]; char s[N]; inline int dis(int a,int b,int c,int d) { return (c-a) * (c-a) + (b-d) * (b-d); } int main(){ scanf("%d%d",&n,&m); cin>>a[1][0]>>a[1][1]>>b[1][0]>>b[1][1]; scanf("%s",s+1); rep(i,1,n) { if(s[i]=='N') a[i+1][0]=a[i][0],a[i+1][1]=a[i][1]+1; else if(s[i]=='S') a[i+1][0]=a[i][0],a[i+1][1]=a[i][1]-1; else if(s[i]=='W') a[i+1][0]=a[i][0]-1,a[i+1][1]=a[i][1]; else if(s[i]=='E') a[i+1][0]=a[i][0]+1,a[i+1][1]=a[i][1]; } scanf("%s",s+1); rep(i,1,m) { if(s[i]=='N') b[i+1][0]=b[i][0],b[i+1][1]=b[i][1]+1; else if(s[i]=='S') b[i+1][0]=b[i][0],b[i+1][1]=b[i][1]-1; else if(s[i]=='W') b[i+1][0]=b[i][0]-1,b[i+1][1]=b[i][1]; else if(s[i]=='E') b[i+1][0]=b[i][0]+1,b[i+1][1]=b[i][1]; } rep(i,1,n+1) rep(j,1,m+1) f[i][j]=2147483647; f[1][1]=0; rep(i,1,n) rep(j,1,m) { f[i+1][j]=min(f[i+1][j],f[i][j]+dis(a[i+1][0],a[i+1][1],b[j][0],b[j][1])); f[i][j+1]=min(f[i][j+1],f[i][j]+dis(a[i][0],a[i][1],b[j+1][0],b[j+1][1])); f[i+1][j+1]=min(f[i+1][j+1],f[i][j]+dis(a[i+1][0],a[i+1][1],b[j+1][0],b[j+1][1])); } printf("%lld\n",f[n+1][m+1]); }