跳到正文

题解

CF536D Tavas in Kansas 题解

jiangly约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
CF536D

版本

  1. v1当前

题意

给定一张 nn (2≤n≤20002\le n\le 2000) 个点 mm (n−1≤m≤105n-1\le m\le 10^5) 条边的无向连通图,每个点有权值 pip_i (∣pi∣≤109|p_i|\le 10^9)。A 和 B 在玩一个游戏,每个人分别拥有一个点 ss, tt。A 先行动,然后两人轮流行动,每次行动选择一个非负整数 xx,然后获取与他的点距离不超过 xx 的所有点的权值。每个点的权值只能被获取一次,每次行动必须获取至少一个点的权值。当所有点的权值都被获取后,游戏结束,得分高者获胜。A, B 均采取最优策略,求胜负情况。

题解

预处理 ss, tt 到每个点的最短路并离散化。设 f(X,Y,p)f(X,Y,p) 表示当前剩下所有到 ss 的距离不小于 XX 且到 tt 的距离不小于 YY 的点,当前先手的人是 pp,所能获得的最大分数。设 cnt(X,Y)\mathrm{cnt}(X,Y) 为到 ss 的距离不小于 XX 且到 tt 的距离不小于 YY 的点数,sum(X,Y)\mathrm{sum}(X,Y) 为它们的权值和,则有

f(X,Y,p)={0if cnt(X,Y)=0,max⁡cnt(X′,Y)<cnt(X,Y){sum(X,Y)−f(X′,Y,1)}if p=0,max⁡cnt(X,Y′)<cnt(X,Y){sum(X,Y)−f(X,Y′,0)}otherwise.f(X,Y,p)=\begin{cases}0&\text{if }\mathrm{cnt}(X,Y)=0,\\\max_{\mathrm{cnt}(X',Y)<\mathrm{cnt}(X,Y)}\{\mathrm{sum}(X,Y)-f(X',Y,1)\}&\text {if }p=0,\\\max_{\mathrm{cnt}(X,Y')<\mathrm{cnt}(X,Y)}\{\mathrm{sum}(X,Y)-f(X,Y',0)\}&\text{otherwise.} \end{cases}

记录部分最小值即可做到 O(N2)O(N^2)。

代码

C++
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1E18;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    vector<int> p(n);
    for (int i = 0; i < n; ++i)
        cin >> p[i];
    vector<vector<pair<int, int>>> e(n);
    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        --u;
        --v;
        e[u].emplace_back(v, w);
        e[v].emplace_back(u, w);
    }
    auto dijkstra = [&](int s) {
        vector<long long> dis(n, 1E18);
        priority_queue<pair<long long, int>> q;
        dis[s] = 0;
        q.emplace(0, s);
        while (!q.empty()) {
            long long d;
            int u;
            tie(d, u) = q.top();
            q.pop();
            d = -d;
            if (d != dis[u])
                continue;
            for (auto ed : e[u]) {
                int v, w;
                tie(v, w) = ed;
                if (dis[v] > d + w) {
                    dis[v] = d + w;
                    q.emplace(-dis[v], v);
                }
            }
        }
        auto values = dis;
        sort(values.begin(), values.end());
        for (int i = 0; i < n; ++i)
            dis[i] = lower_bound(values.begin(), values.end(), dis[i]) - values.begin();
        return vector<int>(dis.begin(), dis.end());
    };
    auto ds = dijkstra(s), dt = dijkstra(t);
    vector<vector<int>> cnt(n + 1, vector<int>(n + 1));
    vector<vector<long long>> sum(n + 1, vector<long long>(n + 1));
    for (int i = 0; i < n; ++i) {
        ++cnt[ds[i]][dt[i]];
        sum[ds[i]][dt[i]] += p[i];
    }
    for (int i = n - 1; i >= 0; --i) {
        for (int j = n - 1; j >= 0; --j) {
            cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
            sum[i][j] += sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1];
        }
    }
    vector<vector<long long>> dpa(n + 1, vector<long long>(n + 1)), dpb(n + 1, vector<long long>(n + 1));
    vector<vector<pair<long long, int>>> qa(n + 1, {{}, {}}), qb(n + 1, {{}, {}});
    auto update = [&](auto x, auto &f) {
        if (x < f[0])
            swap(x, f[0]);
        if (x.second == f[0].second)
            return;
        f[1] = min(f[1], x);
    };
    for (int i = n; i >= 0; --i) {
        for (int j = n; j >= 0; --j) {
            if (cnt[i][j] == 0)
                continue;
            dpa[i][j] = sum[i][j] - (cnt[i][j] == qa[j][0].second ? qa[j][1] : qa[j][0]).first;
            dpb[i][j] = sum[i][j] - (cnt[i][j] == qb[i][0].second ? qb[i][1] : qb[i][0]).first;
            update(make_pair(dpa[i][j], cnt[i][j]), qb[i]);
            update(make_pair(dpb[i][j], cnt[i][j]), qa[j]);
        }
    }
    long long x = dpa[0][0], y = sum[0][0] - x;
    if (x < y)
        cout << "Cry\n";
    else if (x > y)
        cout << "Break a heart\n";
    else
        cout << "Flowers\n";
    return 0;
}
原文