|
@@ -3,7 +3,7 @@
|
|
|
#include <list>
|
|
#include <list>
|
|
|
#include <vector>
|
|
#include <vector>
|
|
|
#include <cmath>
|
|
#include <cmath>
|
|
|
-#include <limits>
|
|
|
|
|
|
|
+#include <queue>
|
|
|
|
|
|
|
|
using namespace std;
|
|
using namespace std;
|
|
|
|
|
|
|
@@ -22,9 +22,9 @@ struct Node
|
|
|
|
|
|
|
|
struct Edge
|
|
struct Edge
|
|
|
{
|
|
{
|
|
|
- Edge(Node *n1, Node *n2, int capacity):n1{n1}, n2{n2}, capacity{capacity}, flow{0}
|
|
|
|
|
|
|
+ Edge(Node *n1, Node *n2, int cap):n1{n1}, n2{n2}, flow{0}
|
|
|
{
|
|
{
|
|
|
- capacity = min(min(n1->capacity, n2->capacity), capacity);
|
|
|
|
|
|
|
+ capacity = min(min(n1->capacity, n2->capacity), cap);
|
|
|
|
|
|
|
|
n1->neighbours.push_back(this);
|
|
n1->neighbours.push_back(this);
|
|
|
n2->neighbours.push_back(this);
|
|
n2->neighbours.push_back(this);
|
|
@@ -35,9 +35,77 @@ struct Edge
|
|
|
int flow;
|
|
int flow;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-int edmonds_karp(vector<Node> m)
|
|
|
|
|
|
|
+Node *other_node(Edge *e, Node *curr)
|
|
|
{
|
|
{
|
|
|
- return 0;
|
|
|
|
|
|
|
+ if(e->n1 == curr)
|
|
|
|
|
+ return e->n2;
|
|
|
|
|
+
|
|
|
|
|
+ return e->n1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
|
|
|
|
|
+{
|
|
|
|
|
+ int max_flow = 0;
|
|
|
|
|
+ // Göra bra krejjer
|
|
|
|
|
+ Edge *es = new Edge(source, source, 200000);
|
|
|
|
|
+ while(true)
|
|
|
|
|
+ {
|
|
|
|
|
+ vector<Edge*> parent(m.size()+1, nullptr);
|
|
|
|
|
+
|
|
|
|
|
+ queue<Node*> q;
|
|
|
|
|
+ q.push(source);
|
|
|
|
|
+ parent[source->id] = es;
|
|
|
|
|
+
|
|
|
|
|
+ while(!q.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ Node *curr = q.front();
|
|
|
|
|
+ q.pop();
|
|
|
|
|
+
|
|
|
|
|
+ for(auto e : curr->neighbours)
|
|
|
|
|
+ {
|
|
|
|
|
+ Node *o = other_node(e, curr);
|
|
|
|
|
+ if(parent[o->id] == nullptr && e->capacity > e->flow)
|
|
|
|
|
+ {
|
|
|
|
|
+ q.push(o);
|
|
|
|
|
+ parent[o->id] = e;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // No (new) way to sink
|
|
|
|
|
+ if(parent[sink->id] == nullptr)
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ int push_flow = 200000;
|
|
|
|
|
+
|
|
|
|
|
+ Edge *e = parent[sink->id];
|
|
|
|
|
+ Node *curr = sink;
|
|
|
|
|
+ while(curr != source)
|
|
|
|
|
+ {
|
|
|
|
|
+ push_flow = min(push_flow, (e->capacity - e->flow) );
|
|
|
|
|
+
|
|
|
|
|
+ curr = other_node(e, curr);
|
|
|
|
|
+ e = parent[curr->id];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ e = parent[sink->id];
|
|
|
|
|
+ curr = sink;
|
|
|
|
|
+ while(curr != source)
|
|
|
|
|
+ {
|
|
|
|
|
+ e->flow += push_flow;
|
|
|
|
|
+
|
|
|
|
|
+ curr = other_node(e, curr);
|
|
|
|
|
+ e = parent[curr->id];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ max_flow += push_flow;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ delete es;
|
|
|
|
|
+
|
|
|
|
|
+ return max_flow;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
int main()
|
|
@@ -53,9 +121,9 @@ int main()
|
|
|
|
|
|
|
|
vector<Node> machines(num_machines);
|
|
vector<Node> machines(num_machines);
|
|
|
machines[0].id = 1;
|
|
machines[0].id = 1;
|
|
|
- machines[0].capacity = INT_MAX;
|
|
|
|
|
|
|
+ machines[0].capacity = 200000;
|
|
|
machines[num_machines-1].id = num_machines;
|
|
machines[num_machines-1].id = num_machines;
|
|
|
- machines[num_machines-1].capacity = INT_MAX;
|
|
|
|
|
|
|
+ machines[num_machines-1].capacity = 200000;
|
|
|
|
|
|
|
|
for(int i = 0; i < num_machines-2; i++)
|
|
for(int i = 0; i < num_machines-2; i++)
|
|
|
{
|
|
{
|
|
@@ -79,16 +147,16 @@ int main()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- cout << edmonds_karp(machines) << endl;
|
|
|
|
|
|
|
+ cout << edmonds_karp(machines, &machines[0], &machines[num_machines-1]) << endl;
|
|
|
|
|
|
|
|
-// for(auto m : machines)
|
|
|
|
|
-// {
|
|
|
|
|
-// cout << m.id << ":" << m.capacity << endl;
|
|
|
|
|
-// for(auto n : m.neighbours)
|
|
|
|
|
-// {
|
|
|
|
|
-// cout << "\t" << n->n1->id << " <--> " << n->n2->id << ": " << n->capacity << endl;
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+ for(auto m : machines)
|
|
|
|
|
+ {
|
|
|
|
|
+ cout << m.id << ":" << m.capacity << endl;
|
|
|
|
|
+ for(auto n : m.neighbours)
|
|
|
|
|
+ {
|
|
|
|
|
+ cout << "\t" << n->n1->id << " <--> " << n->n2->id << " Cap: " << n->capacity << " Flow: " << n->flow << endl;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|