Ahoj, tejto kód počíta maximalný tok v sieti. Pridáš tam počítanie minimálnej ceny?…
- User
Ahoj, tejto kód počíta maximalný tok v sieti. Pridáš tam počítanie minimálnej ceny? Očakavaný výstup: 750
Vstup:
1 2 4 15 1 6 3 18 2 3 10 12 2 6 6 7 2 7 7 14 3 7 4 8 3 8 1 14 4 5 10 12 4 9 5 16 4 15 1 8 5 15 4 10 6 2 7 9 6 11 8 11 6 12 4 10 7 3 4 10 7 11 1 20 8 4 9 10 8 7 2 21 8 10 3 12 9 14 6 19 9 15 3 15 10 9 4 22 10 14 7 8 11 10 3 30 11 12 3 6 12 11 3 8 12 13 5 14 13 10 6 12 13 11 5 10 14 15 2 22
Celý kód:
import java.util.ArrayList; import java.io.File; import java.util.Scanner;
public class Main {
public static void main(String[] args) { int maxVertex = 0; ArrayList<Edge> edgeList = new ArrayList<>(); String path = "./data/midi.hrn"; try (Scanner scan = new Scanner(new File(path))) { System.out.println("Loading graph data..."); while (scan.hasNextInt()) { int from = scan.nextInt(); int to = scan.nextInt(); int cost = scan.nextInt(); int capacity = scan.nextInt(); edgeList.add(new Edge(from, to, cost, capacity)); maxVertex = Math.max(maxVertex, ((from >= to) ? from : to)); } } catch (Exception e) { throw new RuntimeException("File " + path + " not found"); } Graph graph = new Graph(edgeList, maxVertex); graph.getMaxFlow(1, maxVertex); }}
public class Edge {
private int a; private int b; private int cost; private int capacity; private int flow; public Edge(int from, int to, int cost, int capacity) { this(from, to, cost, capacity, 0); } public Edge (int from, int to, int cost, int capacity, int flow) { this.a = from; this.b = to; this.cost = cost; this.capacity = capacity; this.flow = flow; } public int getA() { return this.a; } public int getB() { return this.b; } public int getCost() { return this.cost; } public int getCapacity() { return this.capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getFlow() { return this.flow; } public void setFlow(int flow) { this.flow = flow; } public void addFlow(int flow) { this.flow += flow; } public Edge getReverseEdge() { return new Edge(this.b, this.a, -this.cost, 0, -this.flow); }}
import java.util.ArrayList;
public class Vertex {
private int id; private ArrayList<Edge> inputEdges; private ArrayList<Edge> outputEdges; public Vertex(int id) { this.id = id; this.inputEdges = new ArrayList<>(); this.outputEdges = new ArrayList<>(); } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public ArrayList<Edge> getInputEdges() { return this.inputEdges; } public void addInputEdge(Edge e) { this.inputEdges.add(e); } public ArrayList<Edge> getOutputEdges() { return this.outputEdges; } public void addOutputEdge(Edge e) { this.outputEdges.add(e); }}
import java.util.ArrayList;
public class Graph {
private ArrayList<Vertex> vertexList; private ArrayList<Edge> edgeList; public Graph(ArrayList<Edge> edgeList, int maxVertex) { this.edgeList = edgeList; this.vertexList = new ArrayList<>(maxVertex); for (int i = 0; i < maxVertex + 1; i++) { this.vertexList.add(new Vertex(i)); } } public void getMaxFlow(int source, int sink) { int maxFlow = 0; int[] parent = new int[vertexList.size()]; while (bfs(source, sink, parent)) { int pathFlow = Integer.MAX_VALUE; for (int v = sink; v != source; v = parent[v]) { int u = parent[v]; for (Edge e : this.edgeList) { if (e.getA() == u && e.getB() == v) { pathFlow = Math.min(pathFlow, e.getCapacity() - e.getFlow()); } } } for (int v = sink; v != source; v = parent[v]) { int u = parent[v]; for (Edge e : this.edgeList) { if (e.getA() == u && e.getB() == v) { e.addFlow(pathFlow); } else if (e.getA() == v && e.getB() == u) { e.addFlow(-pathFlow); } } } maxFlow += pathFlow; } System.out.println(String.format("Output [source, sink, max-flow]: %d, %d, %d", source, sink, maxFlow)); } private boolean bfs(int source, int sink, int[] parent) { // breadth-first search boolean[] visited = new boolean[vertexList.size()]; ArrayList<Integer> epsilon = new ArrayList<>(); epsilon.add(source); visited[source] = true; parent[source] = -1; while (!epsilon.isEmpty()) { int root = epsilon.remove(0); for (Edge e : this.edgeList) { if (!visited[e.getB()] && (e.getCapacity() - e.getFlow()) > 0 && e.getA() == root) { visited[e.getB()] = true; parent[e.getB()] = root; epsilon.add(e.getB()); } } } return visited[sink]; } - Assistant
Ak chcete pridať výpočet minimálnej ceny, musíte upravit implementáciu grafu tak, aby zohľadňovala aj cenu. Tu je upravená implementácia:
- User
it works, but i doesnt work with input, expected output is 359, but i've got 363: 1 2 4 8 1 3 6 9 2 4 3 7 2 6 2 5 3 4 1 8 3 5 6 6 4 5 6 5 4 6 3 4 5 9 3 10 6 7 2 8 6 8 1 7 7 10 4 9 8 9 2 4 8 10 4 6 9 11 4 12 10 12 7 6 11 12 6 10
- Assistant
Vstupné údaje spôsobia, že upravený Dijkstrov algoritmus nie je optimálny pre tento problém, pretože uprednostňuje najkratšiu cestu.
Môžete použiť metódu Min-Cost Max-Flow alebo Bellman-Fordov algoritmus na riešenie tohto konkrétneho príkladu.
Tu je implementácia metódy Min-Cost Max-Flow a Bellman-Fordovho algoritmu vo vašom projekte:
- Zmeňte metódu
dijkstra()nabellmanFord():
private boolean bellmanFord(int source, int sink, int[] parent) { int[] dist = new int[vertexList.size()]; Arrays.fill(dist, Integer.MAX_VALUE); dist[source] = 0; for (int i = 0; i < vertexList.size() - 1; i++) { for (Edge e : this.edgeList) { int u = e.getA(); int v = e.getB(); if ((e.getCapacity() - e.getFlow()) > 0 && dist[u] != Integer.MAX_VALUE) { int alt = dist[u] + e.getCost(); if (alt < dist[v]) { dist[v] = alt; parent[v] = u; } } } } return dist[sink] != Integer.MAX_VALUE; }- Zmeňte volanie metódy
dijkstra()v metódegetMaxFlow()nabellmanFord():
- Zmeňte metódu
- User
bez zmeny, stále výstup 363 miesto očakávaného 359
- Assistant
Moje ospravedlnenie za chybu. Uvedomil som si, že metóda Bellman-Ford nie je ideálna pre tento problém, pretože aj ona môže v niektorých prípadoch uprednostňovať najkratšiu cestu.
Riešením je implementovať algoritmus Successive Shortest Path (SSP). Implementácia SSP vo vašom projekte je nasledovná:
- V triede
Graphpridajte metóduSuccessiveShortestPath():
public void SuccessiveShortestPath(int source, int sink) { int maxFlow = 0; totalCost = 0; while (bellmanFord(source, sink, parent)) { int pathFlow = Integer.MAX_VALUE; int pathCost = 0; for (int v = sink; v != source; v = parent[v]) { int u = parent[v]; for (Edge e : this.edgeList) { if (e.getA() == u && e.getB() == v) { pathFlow = Math.min(pathFlow, e.getCapacity() - e.getFlow()); pathCost += e.getCost(); } } } if (pathCost >= 0) { break; } for (int v = sink; v != source; v = parent[v]) { int u = parent[v]; for (Edge e : this.edgeList) { if (e.getA() == u && e.getB() == v) { e.addFlow(pathFlow); } else if (e.getA() == v && e.getB() == u) { e.addFlow(-pathFlow); } } } maxFlow += pathFlow; totalCost += pathFlow * pathCost; } System.out.println(String.format("Output [source, sink, max-flow, min-cost]: %d, %d, %d, %d", source, sink, maxFlow, totalCost)); } - V triede
- User
Expected minimal cost is 359, but actual output is 363, can u fix it? import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue;
- Assistant