UVALive3181-Fixing the Great Wall(区间dp)

题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1182

思路

可以发现这样一个性质:假设A点离起点比B点更远,如果先访问A再访问B一定不会比先访问B再访问A更优,即区间dp。
因此将起点加入到点中,并且按照距离排序。每次做出的选择就是左边离得最近的点或者右边离得最近的点。

状态表示
d[i][j][s]:区间[i, j]内的点已经全部访问过,当s == 0时,处在左端点,s == 1时,处在右端点
转移方程
s == 0: d[i][j][s] = min(d[i + 1][j][0] + (a[i + 1].x - a[i].x) * (w[i] + w[n] - w[j]), d[i + 1][j][1] + (a[j].x - a[i].x) * (w[i] + w[n] - w[j]))
s == 1:  d[i][j][s] = min(d[i][j - 1][0] + (a[j].x - a[i].x) * (w[i - 1] + w[n] - w[j - 1]), d[i][j - 1][1] + (a[j].x - a[j - 1].x) * (w[i - 1] + w[n] - w[j - 1]))

实际上就是每次是从左端点还是右端点转移过来的

细节
  1. 因为每次处理时间的时候有v,容易产生误差,因此先不处理v,最后再一起除以v
  2. 写记搜的话,因为状态最大有100w,会爆栈
  3. 读入的时候scanf()一定要注意结束

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) cout << #x << " = " << x << endl
#define lc o << 1
#define rc o << 1 | 1
#define pl() cout << endl
#define CLR(a, x) memset(a, x, sizeof(a))
#define FILL(a, n, x) for (int i = 0; i < n; i++) a[i] = x

const LL INF = 1e18;
const int maxn = 1010;

struct node {
    LL x, d;
} a[maxn];

LL n, v, x;
int S;
LL d[maxn][maxn][2], w[maxn];

bool cmp(node a, node b) {
    return a.x < b.x;
}

LL solve() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            d[i][j][0] = d[i][j][1] = INF;
    }
    d[S][S][0] = d[S][S][1] = 0;
    for (int l = 1; l < n; l++) {
        for (int i = 1; i <= n - l; i++) {
            int j = i + l;
            LL tc = w[i - 1] + w[n] - w[j];
            for (int s = 0; s <= 1; s++) {
                if (!s) {
                    LL t1 = a[i + 1].x - a[i].x; 
                    LL t2 = a[j].x - a[i].x;
                    d[i][j][s] = min(d[i + 1][j][0] + t1 * (w[i] + w[n] - w[j]), d[i + 1][j][1] + t2 * (w[i] + w[n] - w[j]));    
                } else {
                    LL t1 = a[j].x - a[i].x;
                    LL t2 = a[j].x - a[j - 1].x;
                    d[i][j][s] = min(d[i][j - 1][0] + t1 * (w[i - 1] + w[n] - w[j - 1]), d[i][j - 1][1] + t2 * (w[i - 1] + w[n] - w[j - 1]));
                }
            }
        }
    }
    return min(d[1][n][0], d[1][n][1]);
}

int main() {
    while (scanf("%lld%lld%lld", &n, &v, &x) == 3 && n + v + x != 0) {
        LL tc = 0;
        for (int i = 1; i <= n; i++) {
            LL c;
            scanf("%lld%lld%lld", &a[i].x, &c, &a[i].d);
            tc += (c * v);
        }
        a[++n].x = x, a[n].d = 0;
        sort(a + 1, a + 1 + n, cmp);
        for (int i = 1; i <= n; i++) 
            if (a[i].x == x) {
                S = i;
                break;
            }
        w[0] = 0, w[n + 1] = 0;
        for (int i = 1; i <= n; i++) w[i] = w[i - 1] + a[i].d;
        LL ans = solve();
        ans += tc;
        ans /= v;
        printf("%lld\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值