To solve this problem, we need to adjust the amount of soil in each pot from its initial amount Ai to the desired amount Bi. We have three operations:
- Buy new soil and add it to a pot.
- Remove excess soil from a pot and discard it.
- Transfer soil from one pot to another.
Our goal is to perform these operations at the minimum total cost.
Here’s the approach:
- Calculate the net change required for each pot:
Di = Bi - Ai. This indicates if we need to add (Di > 0), remove (Di < 0), or keep the soil the same (Di = 0).
- Identify surplus and shortage pots:
- Surplus pots: Pots where
Di < 0 (we have extra soil).
- Shortage pots: Pots where
Di > 0 (we need more soil).
- Create a network graph representing possible soil transfers:
- Nodes are pots, and edges represent possible transfers.
- Edge costs are the cost per unit of transferring soil, which depends on the distance between pots (
Z * abs(i - j)).
We use a minimum cost flow algorithm to minimize the total cost of adjusting the soil amounts:
- Supplies and Demands:
- Surplus pots supply excess soil (
-Di units).
- Shortage pots demand soil (
Di units).
- Edge Costs:
- If the cost of transferring soil between pots
i and j (Z * abs(i - j)) is less than the combined cost of buying and discarding soil (X + Y), we consider transferring soil between these pots.
- We also include edges for buying new soil (
X per unit) and discarding excess soil (Y per unit).
By applying the min-cost flow algorithm on this graph, we find the minimum total cost to adjust all pots to their desired soil amounts.
Here’s the implementation in Java:
import java.util.*;
import java.io.*;
public class Main {
static final int INF = Integer.MAX_VALUE / 2;
static int N, X, Y, Z;
static int[] Ai, Bi, Di;
static List<List<Edge>> graph;
static int source, sink;
static class Edge {
int to, rev, capacity, cost;
Edge(int to, int rev, int capacity, int cost) {
this.to = to; this.rev = rev; this.capacity = capacity; this.cost = cost;
}
}
public static void addEdge(int from, int to, int capacity, int cost) {
graph.get(from).add(new Edge(to, graph.get(to).size(), capacity, cost));
graph.get(to).add(new Edge(from, graph.get(from).size() - 1, 0, -cost));
}
public static int[] minCostFlow(int V, int s, int t, int maxf) {
int flow = 0;
int flowCost = 0;
int[] h = new int[V]; // Potential
while (flow < maxf) {
PriorityQueue<long[]> pq = new PriorityQueue<>(Comparator.comparingLong(a -> a[1]));
int[] dist = new int[V];
Arrays.fill(dist, INF);
dist[s] = 0;
pq.add(new long[]{s, 0});
int[] prevv = new int[V];
int[] preve = new int[V];
while (!pq.isEmpty()) {
long[] p = pq.poll();
int v = (int) p[0];
if (dist[v] < p[1]) continue;
for (int i = 0; i < graph.get(v).size(); i++) {
Edge e = graph.get(v).get(i);
if (e.capacity > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
pq.add(new long[]{e.to, dist[e.to]});
}
}
}
if (dist[t] == INF) {
break; // Cannot flow anymore
}
for (int v = 0; v < V; v++) h[v] += dist[v];
int d = maxf - flow;
for (int v = t; v != s; v = prevv[v]) {
d = Math.min(d, graph.get(prevv[v]).get(preve[v]).capacity);
}
flow += d;
flowCost += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge e = graph.get(prevv[v]).get(preve[v]);
e.capacity -= d;
graph.get(v).get(e.rev).capacity += d;
}
}
return new int[]{flow, flowCost};
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
X = sc.nextInt();
Y = sc.nextInt();
Z = sc.nextInt();
Ai = new int[N];
Bi = new int[N];
Di = new int[N];
for (int i = 0; i < N; i++) {
Ai[i] = sc.nextInt();
Bi[i] = sc.nextInt();
Di[i] = Bi[i] - Ai[i];
}
// Construct the graph
graph = new ArrayList<>();
int V = N + 2;
source = V - 2;
sink = V - 1;
for (int i =0; i < V; i++) {
graph.add(new ArrayList<>());
}
int totalDemand = 0;
int totalSupply = 0;
for (int i = 0; i < N; i++) {
if (Di[i] > 0) { // Need more soil
addEdge(source, i, Di[i], 0);
// Option to buy new soil
addEdge(i, sink, Di[i], X);
totalDemand += Di[i];
} else if (Di[i] < 0) { // Has surplus soil
addEdge(i, sink, -Di[i], 0);
// Option to discard surplus soil
addEdge(source, i, -Di[i], Y);
totalSupply += -Di[i];
}
}
// Edge from surplus pots to shortage pots
for (int i = 0; i < N; i++) {
if (Di[i] < 0) { // Surplus pot
for (int j = 0; j < N; j++) {
if (Di[j] > 0) { // Shortage pot
int transferCost = Z * Math.abs(i - j);
int minCost = Math.min(transferCost, X + Y);
addEdge(i, j, INF, minCost);
}
}
}
}
int maxFlow = totalDemand + totalSupply; // This is an upper bound
int[] result = minCostFlow(V, source, sink, maxFlow);
System.out.println(result[1]);
}
}
This code implements the min-cost flow algorithm using the successive shortest augmenting path method, suitable for small graphs (since N ≤ 100).