跳到正文

题解

题解:P6054 [RC-02] 开门大吉

yyyx_约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年3月7日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 9 分钟
获赞
3
题目
P6054

版本

  1. v1当前

本题解主要警示后人,如果你完全不懂,建议去楼上看其他大佬的题解。

本质与 P3227 [HNOI2013] 切糕 一致。

你考虑将选择哪套题目转化为网络流上的割边,令题目代价为边的流量,则最小割即为答案。

对于每条约束,连边 (j,p),(i,p+k)(j,p),(i,p+k),权值为 ∞\infty。

这样使得 idi−idj<kid_i-id_j<k 时不能仅割断 idi,idjid_i,id_j 两条边使得图不连通。容易证明多选一条边是不优的。

以下是作者整合和亲身经历的错误:

  1. 题意:没说明若第 ii 题答错,直接结束答题。(怎么还没修啊)

  2. 题意:是“大 kk”,而不是“差大于 kk”。

  3. 如使用 cout,若不加 std::fixed 会用科学计数法输出。

  4. double 自带的 #INF 相减的值为 NaN。

  5. 用总流量判无解,而不是一次增广的流量。

  6. 多测清空要一直到 TT(网络流汇点)。

  7. 自定义的 Point(x,y),即二维点转点编号应如下(除非你不是用 (i,m+1)(i,m+1) 虚点来避免取不合法点的):

C++
#define Point(x, y) ((x - 1) * 82 + y)
C++
// 错误示范
#define Point(x, y) ((x - 1) * 80 + y)
  1. 建边不能只建距离为 kk 的,要将边缘长度不足 kk 的继续连边,因为他们同样不满足条件,具体可以看以下代码区别:
C++
// 正确的
for (int i = max(1, 1 - k); i <= m + 1; i++)
    add_edge(Point(y, i), Point(x, min(i + k, m + 1)), inf);
C++
// 错误的
for (int i = 1; i <= m + 1; i++)
    if (i + k <= m + 1 && i + k >= 1)
        add_edge(Point(y, i), Point(x, i + k), inf);

附代码。

C++
#include <bits/stdc++.h>
using namespace std;

template <typename T>
inline void read(T &x)
{
    x = 0;
    register char c = getchar();
    register short f = 1;
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}

template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
    read(x), read(temps...);
}

const int N = 82;
typedef double ll;
const ll inf = 1e18;
const ll eps = 1e-9;

int S, T;
vector<int> tag[N * N * 4], dep, res;
vector<pair<int, ll>> g;
#define Point(x, y) ((x - 1) * 82 + y)

void add_edge(int x, int y, ll w)
{
    tag[x].emplace_back(g.size());
    g.emplace_back(y, w);
    tag[y].emplace_back(g.size());
    g.emplace_back(x, 0);
}

#define is0(x) (x < eps ? 1 : 0)

bool bfs()
{
    dep.assign(T + 1, 0);
    res.assign(T + 1, 0);
    queue<int> q;
    dep[S] = 1;
    q.emplace(S);
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        for (auto id : tag[x])
        {
            auto [y, w] = g[id];
            if (dep[y] || is0(w))
                continue;
            dep[y] = dep[x] + 1;
            if (y == T)
                return 1;
            q.emplace(y);
        }
    }
    return dep[T];
}

ll dfs(int x, ll flow)
{
    if (x == T)
        return flow;
    ll cnt = 0;
    for (int &i = res[x]; i < tag[x].size(); i++)
    {
        int id = tag[x][i];
        auto &[y, w] = g[id];
        if (dep[x] + 1 != dep[y] || is0(w))
            continue;
        ll fw = dfs(y, min(flow, w));
        if (fw)
        {
            cnt += fw;
            flow -= fw;
            w -= fw;
            g[id ^ 1].second += fw;
            if (!flow)
                break;
        }
    }
    return cnt;
}

int TwT, n, m, P, Q;
ll v[N][N];

ll dinic()
{
    ll ans = 0;
    while (bfs())
    {
        ans += dfs(S, inf);
        if (ans >= inf)
            return -1;
    }
    return ans;
}

void clear()
{
    for (int i = 0; i <= T; i++)
        tag[i].clear();
    g.clear();
}

signed main()
{
    read(TwT);
    while (TwT--)
    {
        read(n, m, P, Q);
        vector<int> c(P);
        for (auto &x : c)
            read(x);
        for (int j = 1; j <= m; j++)
            for (int i = 1; i <= n; i++)
            {
                v[i][j] = 0;
                ll x = 0, y = 1;
                for (int k = 0; k < P; k++)
                {
                    scanf("%lf", &x);
                    v[i][j] += (y *= x) * c[k];
                }
            }
        S = 0, T = Point(n, m + 1) + 1;
        clear();
        for (int i = 1; i <= n; i++)
        {
            add_edge(S, Point(i, 1), inf);
            add_edge(Point(i, m + 1), T, inf);
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                add_edge(Point(i, j), Point(i, j + 1), v[i][j]);
        for (int x, y, k; Q--;)
        {
            read(x, y, k);
            for (int i = max(1, 1 - k); i <= m + 1; i++)
                add_edge(Point(y, i), Point(x, min(i + k, m + 1)), inf);
        }
        auto ans = dinic();
        if (ans < 0)
            puts("-1");
        else
            printf("%.9lf\n", ans);
    }

    return 0;
}
原文