bzoj4510: [Usaco2016 Jan]Radio Contact

bzoj4510: [Usaco2016 Jan]Radio Contact

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 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

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]);
}

时间: 2024-10-06 10:42:34

bzoj4510: [Usaco2016 Jan]Radio Contact的相关文章

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

P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 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 to

bzoj4509【Usaco2016 Jan】Angry Cows

4509: [Usaco2016 Jan]Angry Cows Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 83  Solved: 38 [Submit][Status][Discuss] Description Bessie the cow has designed what she thinks will be the next big hit video game: "Angry Cows". The premise, whi

bzoj 4506: [Usaco2016 Jan]Fort Moo

4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any good fort, this one needs to start with a sturdy frame. Bessie wants to build a frame in the shape of a one-meter-wide rectangular outline, atop which

bzoj4511: [Usaco2016 Jan]Subsequences Summing to Sevens

前缀和. 设f[i]为前缀和%7=i的第一个点.那么答案就是max(i-f[s[i]%7])了. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn = 50000 + 10; int a[maxn],s[maxn]; int f[10],n,ans; int main() { for(int i=1;i<7;i++) f[i]=-1;

bzoj4525: [Usaco2016 Jan]Angry Cows

二分. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn = 50000 + 10; int n,k,l,r,mid,ans,d; int a[maxn]; bool check(int dist) { dist=2*dist; int d=0,sum=1; for(int i=2;i<=n;i++) { if(a[i]-a[i-1]

bzoj4512: [Usaco2016 Jan] Build Gates

题目大意:给个序列,求最长的连续子序列使其为7的倍数 又是一道令人欢喜的不用怎么用脑的水题.. 边读入,边计算前缀和 分别保存前缀和%7结果为1,2,3,4,5,6的第一次的位置 然后减一减就知道长度啦. 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int n,t,last[8],ans; 6 long long sum[50010]; 7

[BZOJ4506] [Usaco2016 Jan]Fort Moo(DP?)

传送门 总之可以先预处理出来每个位置最多往上延伸多少 枚举两行,看看夹在这两行中间的列最大能构成多大的矩形 可以看出,必须得在一个两行都没有X的区间才有可能构成最大的答案 那么可以把这些区间处理出来,在看看这些区间中的点最左边和最右边的能从下面那一行向上延伸到上面那一行的点,更新ans即可 #include <cstdio> #define N 201 #define max(x, y) ((x) > (y) ? (x) : (y)) int n, m, ans, p; int h[N]

【翻译贴】上海市第八届星光计划(2019年)技能大赛网站设计(中职组)决赛题目

两年一度的上海市星光计划比赛被誉为职业技能大赛的奥林匹克,比赛分为中职组和高职组 随着若干年的发展,比赛的题目强度逐年提高,逐步向世界职业技能大赛的比赛强度靠拢 这次翻译的题目是上海市第八届星光计划(2019年)技能大赛网站设计(中职教师组)的决赛题目.全文是英文的,学生组的题目和教师组差不多,个别地方降低了要求.故这次翻译了教师组的题目. 决赛要求 模块 A:站点设计 一.竞赛时间 150 min 二.竞赛任务及要求 1. INTRODUCTION: Frankfurt Internation

Radio Basics for RFID

Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09