Skip to content

Commit 1aaa9fc

Browse files
authored
2022: new max flow skel (acs-pa#34)
1 parent d4df372 commit 1aaa9fc

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

skel/lab12/cpp/task-1/main.cpp

+30-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ using namespace std;
44
// numarul maxim de noduri
55
#define NMAX 1005
66

7+
// valoare mai mare decat max_flow
8+
#define INF (1 << 30)
9+
10+
// structura folosita pentru a stoca daca exista drum de ameliorare
11+
// si care este acesta.
12+
struct AugmentedBFS {
13+
bool has_path;
14+
vector<pair<int, int>> path;
15+
AugmentedBFS(bool has_path, const vector<pair<int, int>>& path)
16+
: has_path(has_path)
17+
, path(path) { }
18+
19+
operator bool() const { return has_path; }
20+
};
21+
722
class Task {
823
public:
924
void solve() {
@@ -18,23 +33,23 @@ class Task {
1833
// adj[i] = lista de adiacenta a nodului i
1934
vector<int> adj[NMAX];
2035

21-
// cap[i][j] = capacitatea arcului i -> j
22-
int cap[NMAX][NMAX];
36+
// c[i][j] = capacitatea arcului i -> j
37+
int c[NMAX][NMAX];
2338

2439
void read_input() {
2540
ifstream fin("in");
2641
fin >> n >> m;
27-
memset(cap, 0, sizeof cap);
28-
for (int i = 1, x, y, c; i <= m; i++) {
29-
// x -> y de capacitate c
30-
fin >> x >> y >> c;
31-
adj[x].push_back(y);
32-
adj[y].push_back(x);
42+
memset(c, 0, sizeof(c));
43+
for (int i = 1, u, v, capacity; i <= m; i++) {
44+
// x -> y de capacitate cap
45+
fin >> u >> v >> capacity;
46+
adj[u].push_back(v);
47+
adj[v].push_back(u); // stocam si arcul invers
3348

34-
// Presupunem existenta mai multor arce x -> y cu capacitati c1, c2, ...
49+
// Presupunem existenta mai multor arce u -> v cu capacitati c1, c2, ...
3550
// Comprimam intr-un singur arc x -> y cu capacitate
36-
// cap[x][y] = c1 + c2 + ...
37-
cap[x][y] += c;
51+
// c[x][y] = c1 + c2 + ...
52+
c[u][v] += capacity;
3853
}
3954
fin.close();
4055
}
@@ -47,12 +62,11 @@ class Task {
4762
//
4863
// In adj este stocat graful neorientat obtinut dupa ce se elimina orientarea
4964
// arcelor, iar in cap sunt stocate capacitatile arcelor.
50-
// De exemplu, un arc (x, y) de capacitate c va fi tinut astfel:
51-
// cap[x][y] = c, adj[x] contine y, adj[y] contine x.
65+
// De exemplu, un arc (u, v) de capacitate cap va fi tinut astfel:
66+
// c[u][v] = cap, adj[u] contine v, adj[v] contine u.
5267
//
53-
int max_flow = 0;
54-
55-
return max_flow;
68+
int total_flow = 0;
69+
return total_flow;
5670
}
5771

5872
void print_output(int result) {

skel/lab12/java/task-1/src/Main.java

+51-19
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import java.io.FileWriter;
55
import java.io.IOException;
66
import java.io.PrintWriter;
7-
import java.util.Scanner;
7+
import java.lang.Math;
8+
import java.text.CollationElementIterator;
89
import java.util.ArrayList;
10+
import java.util.Arrays;
911
import java.util.Collections;
12+
import java.util.LinkedList;
13+
import java.util.Queue;
14+
import java.util.Scanner;
1015

1116
public class Main {
1217
static class Task {
@@ -16,6 +21,9 @@ static class Task {
1621
// numarul maxim de noduri
1722
public static final int NMAX = 1005;
1823

24+
// valoare mai mare decat maxFlow
25+
public static final int INF = (int) 10e9;
26+
1927
// n = numar de noduri, m = numar de muchii
2028
int n, m;
2129

@@ -24,7 +32,30 @@ static class Task {
2432
ArrayList<Integer> adj[] = new ArrayList[NMAX];
2533

2634
// cap[i][j] = capacitatea arcului i -> j
27-
int cap[][];
35+
int c[][];
36+
37+
public class Edge {
38+
public int node;
39+
public int neigh;
40+
41+
Edge(int _node, int _neigh) {
42+
node = _node;
43+
neigh = _neigh;
44+
}
45+
46+
}
47+
48+
// structura folosita pentru a stoca daca exista drum de ameliorare
49+
// si care este acesta.
50+
public class AugmentedPath {
51+
boolean hasPath;
52+
ArrayList<Edge> path;
53+
54+
AugmentedPath(boolean _hasPath, ArrayList<Edge> _path) {
55+
hasPath = _hasPath;
56+
path = _path;
57+
}
58+
}
2859

2960
public void solve() {
3061
readInput();
@@ -34,27 +65,29 @@ public void solve() {
3465
private void readInput() {
3566
try {
3667
Scanner sc = new Scanner(new BufferedReader(new FileReader(
37-
INPUT_FILE)));
68+
INPUT_FILE)));
3869
n = sc.nextInt();
3970
m = sc.nextInt();
4071

41-
cap = new int[n + 1][n + 1];
4272
for (int i = 1; i <= n; i++) {
4373
adj[i] = new ArrayList<>();
4474
}
75+
76+
c = new int[n + 1][n + 1];
77+
4578
for (int i = 1; i <= m; i++) {
4679
// x -> y de capacitate c
47-
int x, y, c;
48-
x = sc.nextInt();
49-
y = sc.nextInt();
50-
c = sc.nextInt();
51-
adj[x].add(y);
52-
adj[y].add(x);
80+
int u, v, capacity;
81+
u = sc.nextInt();
82+
v = sc.nextInt();
83+
capacity = sc.nextInt();
84+
adj[u].add(v);
85+
adj[v].add(u);
5386

5487
// Presupunem existenta mai multor arce x -> y cu capacitati c1, c2, ...
5588
// Comprimam intr-un singur arc x -> y cu capacitate
5689
// cap[x][y] = c1 + c2 + ...
57-
cap[x][y] += c;
90+
c[u][v] += capacity;
5891
}
5992
sc.close();
6093
} catch (IOException e) {
@@ -65,7 +98,7 @@ private void readInput() {
6598
private void writeOutput(int result) {
6699
try {
67100
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
68-
OUTPUT_FILE)));
101+
OUTPUT_FILE)));
69102
pw.printf("%d\n", result);
70103
pw.close();
71104
} catch (IOException e) {
@@ -80,16 +113,15 @@ private int getResult() {
80113
// Destinatia este nodul n.
81114
//
82115
// In adj este stocat graful neorientat obtinut dupa ce se elimina orientarea
83-
// arcelor, iar in cap sunt stocate capacitatile arcelor.
84-
// De exemplu, un arc (x, y) de capacitate z va fi tinut astfel:
85-
// cap[x][y] = z, adj[x] contine y, adj[y] contine x.
116+
// arcelor, iar in c sunt stocate capacitatile arcelor.
117+
// De exemplu, un arc (x, y) de capacitate cap va fi tinut astfel:
118+
// c[x][y] = cap, adj[x] contine y, adj[y] contine x.
86119
//
87-
int maxFlow = 0;
88-
89-
return maxFlow;
120+
int totalFlow = 0;
121+
return totalFlow;
90122
}
91-
}
92123

124+
}
93125
public static void main(String[] args) {
94126
new Task().solve();
95127
}

0 commit comments

Comments
 (0)