BZOJ4510——[Usaco2016 Jan]Radio Contact

来源:互联网 发布:时尚女装淘宝店铺推荐 编辑:程序博客网 时间:2024/05/21 02:21

1、题意:John和Bessie分别从(fx,fy)和(bx,by)出发,他们通过无线电通讯,单位时间消耗能量大小等于两人距离的平方(但他们同时在出发点的开始时刻的能量不算),为了节约能量,他们尽量靠在一起。单位时间内John和Bessie都可以选择走或不走。问最小使用能量大小。 转自huanghongxun
2、分析:把两个人走的步数作为状态,做一遍dp即可

#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;#define M 1010#define LL long longinline int read(){    char ch = getchar(); int x = 0, f = 1;    while(ch < '0' || ch > '9'){        if(ch == '-') f = -1;        ch = getchar();    }    while('0' <= ch && ch <= '9'){        x = x * 10 + ch - '0';        ch = getchar();    }    return x * f;}struct Node{    int x, y;} f[M], b[M];char str[M];int dx[] = {0, 1, 0, -1};int dy[] = {1, 0, -1, 0};LL dp[M][M];inline LL sqr(int x){    return (LL)x * (LL)x;}inline int get_direction(char ch){    if(ch == 'N') return 0;    if(ch == 'E') return 1;    if(ch == 'S') return 2;    return 3;} inline LL dis(Node x, Node y){    return sqr(x.x - y.x) + sqr(x.y - y.y);}int main(){    //freopen("0input.in", "r", stdin);    int n = read(), m = read();    f[0].x = read(), f[0].y = read();    b[0].x = read(), b[0].y = read();    scanf("%s", str); int x = f[0].x, y = f[0].y;    for(int i = 0; i < n; i ++){        f[i + 1].x = (x += dx[get_direction(str[i])]);        f[i + 1].y = (y += dy[get_direction(str[i])]);     }    scanf("%s", str); x = b[0].x, y = b[0].y;    for(int i = 0; i < m; i ++){        b[i + 1].x = (x += dx[get_direction(str[i])]);        b[i + 1].y = (y += dy[get_direction(str[i])]);    }    memset(dp, 0x7f, sizeof(dp));    dp[0][0] = 0;    for(int i = 0; i <= n; i ++){        for(int j = 0; j <= m; j ++){            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dis(f[i + 1], b[j]));            dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + dis(f[i], b[j + 1]));            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + dis(f[i + 1], b[j + 1]));        }     }    printf("%lld\n", dp[n][m]);    return 0;}
0 0
原创粉丝点击