题解:P17199 [KOI 2026 #2] 工厂

· · 题解

这道题按照题目模拟就行了,不会很难也不会很简单。下列是参考代码:

include <bits/stdc++.h>

using namespace std; using int64 = long long; struct Applicant { int skill; int contribution; int salary; int day; int id; };

struct RelabelToFront { struct Edge { int to; int reverseIndex; int64 capacity; };

int vertexCount;
vector<vector<Edge>> graph;
vector<int> height;
vector<int> currentEdge;
vector<int64> excess;

explicit RelabelToFront(int vertexCount)
    : vertexCount(vertexCount),
      graph(vertexCount),
      height(vertexCount, 0),
      currentEdge(vertexCount, 0),
      excess(vertexCount, 0) {}

void addUndirectedEdge(
    int first,
    int second,
    int64 capacity
) {
    Edge forward{
        second,
        static_cast<int>(graph[second].size()),
        capacity
    };

    Edge backward{
        first,
        static_cast<int>(graph[first].size()),
        capacity
    };

    graph[first].push_back(forward);
    graph[second].push_back(backward);
}

void push(int vertex, Edge &edge) {
    int64 sent =
        min(excess[vertex], edge.capacity);

    if (sent == 0) {
        return;
    }

    edge.capacity -= sent;

    graph[edge.to][edge.reverseIndex].capacity
        += sent;

    excess[vertex] -= sent;
    excess[edge.to] += sent;
}

void relabel(int vertex) {
    int minimumHeight =
        numeric_limits<int>::max();

    for (const Edge &edge : graph[vertex]) {
        if (edge.capacity > 0) {
            minimumHeight =
                min(minimumHeight,
                    height[edge.to]);
        }
    }

    height[vertex] = minimumHeight + 1;
}

void discharge(int vertex) {
    while (excess[vertex] > 0) {
        if (currentEdge[vertex] ==
            static_cast<int>(
                graph[vertex].size()
            )) {
            relabel(vertex);
            currentEdge[vertex] = 0;
            continue;
        }

        Edge &edge =
            graph[vertex]
                 [currentEdge[vertex]];

        if (edge.capacity > 0 &&
            height[vertex] ==
                height[edge.to] + 1) {
            push(vertex, edge);
        } else {
            ++currentEdge[vertex];
        }
    }
}

int64 maximumFlow(int source, int sink) {
    height[source] = vertexCount;

    for (Edge &edge : graph[source]) {
        int64 sent = edge.capacity;

        if (sent == 0) {
            continue;
        }

        edge.capacity = 0;

        graph[edge.to][edge.reverseIndex]
            .capacity += sent;

        excess[source] -= sent;
        excess[edge.to] += sent;
    }

    vector<int> vertices;
    vertices.reserve(vertexCount - 2);

    for (int vertex = 0;
         vertex < vertexCount;
         ++vertex) {
        if (vertex != source &&
            vertex != sink) {
            vertices.push_back(vertex);
        }
    }

    int index = 0;

    while (index <
           static_cast<int>(vertices.size())) {
        int vertex = vertices[index];
        int oldHeight = height[vertex];

        discharge(vertex);

        if (height[vertex] > oldHeight) {
            vertices.erase(
                vertices.begin() + index
            );

            vertices.insert(
                vertices.begin(), vertex
            );

            index = 0;
        } else {
            ++index;
        }
    }

    return excess[sink];
}

vector<char> sourceSide(int source) const {
    vector<char> reachable(
        vertexCount, false
    );

    queue<int> queue;
    reachable[source] = true;
    queue.push(source);

    while (!queue.empty()) {
        int vertex = queue.front();
        queue.pop();

        for (const Edge &edge :
             graph[vertex]) {
            if (edge.capacity > 0 &&
                !reachable[edge.to]) {
                reachable[edge.to] = true;
                queue.push(edge.to);
            }
        }
    }

    return reachable;
}

};

int main() { ios::sync_with_stdio(false); cin.tie(nullptr);

int n, days;
cin >> n >> days;

vector<Applicant> applicants(n);

for (int i = 0; i < n; ++i) {
    cin >> applicants[i].skill
        >> applicants[i].contribution
        >> applicants[i].salary
        >> applicants[i].day;

    applicants[i].id = i;
}

sort(
    applicants.begin(),
    applicants.end(),
    [](const Applicant &first,
       const Applicant &second) {
        return first.skill < second.skill;
    }
);

const int source = n;
const int sink = n + 1;
const int64 INF = (1LL << 50);

vector<int> currentParityNode(
    days + 1, source
);

vector<int> previousNode(n, source);
vector<int64> linearCoefficient(n, 0);
vector<vector<int64>> cutCapacity(
    n + 1,
    vector<int64>(n + 1, 0)
);

auto addCutTerm =
    [&](int first,
        int second,
        int64 capacity) {
        if (first == second ||
            capacity == 0) {
            return;
        }

        if (first > second) {
            swap(first, second);
        }

        cutCapacity[first][second]
            += capacity;
    };

for (int position = 0;
     position < n;
     ++position) {
    const Applicant &applicant =
        applicants[position];

    int node = applicant.id;
    int oldNode =
        currentParityNode[applicant.day];

    previousNode[node] = oldNode;
    addCutTerm(
        oldNode,
        node,
        applicant.salary
    );

    if (oldNode != source) {
        linearCoefficient[oldNode]
            -= applicant.contribution;
    }

    linearCoefficient[node]
        += applicant.contribution;

    currentParityNode[applicant.day]
        = node;

    if (position + 1 == n) {
        continue;
    }

    int64 skillDifference =
        applicants[position + 1].skill -
        applicant.skill;
    addCutTerm(
        source,
        currentParityNode[1],
        skillDifference
    );
    for (int day = 1;
         day < days;
         ++day) {
        addCutTerm(
            currentParityNode[day],
            currentParityNode[day + 1],
            skillDifference
        );
    }
    addCutTerm(
        source,
        currentParityNode[days],
        skillDifference
    );
}
for (int day = 1;
     day <= days;
     ++day) {
    addCutTerm(
        source,
        currentParityNode[day],
        INF
    );
}

int64 constantTerm = 0;

for (int node = 0; node < n; ++node) {
    int64 coefficient =
        linearCoefficient[node];

    if (coefficient >= 0) {
        addCutTerm(
            source,
            node,
            coefficient
        );
    }
}

RelabelToFront flow(n + 2);

for (int first = 0;
     first <= n;
     ++first) {
    for (int second = first + 1;
         second <= n;
         ++second) {
        if (cutCapacity[first][second]
            > 0) {
            flow.addUndirectedEdge(
                first,
                second,
                cutCapacity[first][second]
            );
        }
    }
}

for (int node = 0; node < n; ++node) {
    if (linearCoefficient[node] < 0) {
        constantTerm +=
            linearCoefficient[node];
        flow.addUndirectedEdge(
            node,
            sink,
            -linearCoefficient[node]
        );
    }
}

int64 minimumCost =
    constantTerm +
    flow.maximumFlow(source, sink);

vector<char> sourceSide =
    flow.sourceSide(source);

vector<int> hired;

for (const Applicant &applicant :
     applicants) {
    int node = applicant.id;

    if (sourceSide[node] !=
        sourceSide[previousNode[node]]) {
        hired.push_back(node + 1);
    }
}

cout << -minimumCost << '\n';
cout << hired.size() << '\n';

for (int i = 0;
     i < static_cast<int>(hired.size());
     ++i) {
    if (i > 0) {
        cout << ' ';
    }

    cout << hired[i];
}

cout << '\n';
return 0;