-
Notifications
You must be signed in to change notification settings - Fork 1
/
lemonade.cc
80 lines (67 loc) · 1.89 KB
/
lemonade.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <lemon/list_graph.h>
#include <lemon/preflow.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
using namespace lemon;
int main(int argc, char **argv)
{
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <width>" << std::endl;
exit(1);
}
int width = atoi(argv[1]);
int depth = width / 2;
srand(time(NULL));
ListDigraph g;
auto s = g.addNode();
auto t = g.addNode();
std::vector<std::vector<ListDigraph::Node> > node_mat;
lemon::ListDigraph::ArcMap<int> cap(g);
for (int z = 0; z < depth; z++) {
std::vector<ListDigraph::Node> node_row;
for (int y = 0; y < width - 2*z; y++) {
if (y <= width - 2*z) {
// Create a node
auto node = g.addNode();
node_row.push_back(node);
// Determine value, and link to source or target
auto value = (rand() - RAND_MAX / 2) / (RAND_MAX / 100);
if (value > 0) {
cap[g.addArc(s, node)] = value;
} else {
cap[g.addArc(node, t)] = -value;
}
// Link to nodes above
if (z > 0) {
cap[g.addArc(node, node_mat[z-1][y])] = INT_MAX;
cap[g.addArc(node, node_mat[z-1][y+1])] = INT_MAX;
cap[g.addArc(node, node_mat[z-1][y+2])] = INT_MAX;
}
}
}
node_mat.push_back(node_row);
}
// Now call the min cut solver.
auto preflower = Preflow<lemon::ListDigraph>(g, cap, s, t);
preflower.runMinCut();
int n = 0;
for (auto row = node_mat.begin(); row != node_mat.end(); ++row) {
// Shift the row over a bunch.
for (int i = 0; i < n; i++)
std::cout << "X";
for (auto node = row->begin(); node != row->end(); ++node) {
if (preflower.minCut(*node)) {
std::cout << " ";
} else {
std::cout << "X";
}
}
for (int i = 0; i < n; i++)
std::cout << "X";
n++;
std::cout << std::endl;
}
return 0;
}