Selaa lähdekoodia

Ytterligare en version som inte fungerar

Jonatan Gezelius 6 vuotta sitten
vanhempi
commit
a01acbc8c8
1 muutettua tiedostoa jossa 48 lisäystä ja 15 poistoa
  1. 48 15
      p11506/p11506.cpp

+ 48 - 15
p11506/p11506.cpp

@@ -11,11 +11,12 @@ struct Edge;
 
 struct Node
 {
-    Node(){}
-    Node(unsigned int size):neighbours{size} {}
+    Node():flow{0} {}
+    Node(unsigned int size):flow{0}, neighbours{size} {}
 
     int id;
     int capacity;
+    int flow;
 
     vector<struct Edge*> neighbours;
 };
@@ -45,17 +46,24 @@ Node *other_node(Edge *e, Node *curr)
 
 int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
 {
+    // Edmonds karp-variant
     int max_flow = 0;
-    // Göra bra krejjer
+
+    // Lägg in en båge från source till sig själv för att undvika specialfall
     Edge *es = new Edge(source, source, 200000);
+
+    // Medan vi inte har hittat alla intressanta vägar från source till sink
     while(true)
     {
+        // Förbered vektor att spara alla bågar vi gått till respektive nod
         vector<Edge*> parent(m.size()+1, nullptr);
 
+        // Kö för bredden först
         queue<Node*> q;
         q.push(source);
-        parent[source->id] = es;
+        parent[source->id] = es; // Lägg in special-bågen för source
 
+        // Bredden först typ
         while(!q.empty())
         {
             Node *curr = q.front();
@@ -63,10 +71,14 @@ int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
 
             for(auto e : curr->neighbours)
             {
+                // Pekare till noden o på andra sidan bågen e
                 Node *o = other_node(e, curr);
-                if(parent[o->id] == nullptr && e->capacity > e->flow)
+                if(parent[o->id] == nullptr && e->capacity > e->flow && o->capacity > o->flow)
                 {
+                    // Ifall vi inte gått vägen innan, och den fortfarande har kapacitet kvar
+                    // Lägg till noden till bredden först
                     q.push(o);
+                    // och spara bågen vi tog för att hamna i nuvarande nod
                     parent[o->id] = e;
                 }
             }
@@ -77,8 +89,9 @@ int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
         if(parent[sink->id] == nullptr)
             break;
 
-        int push_flow = 200000;
+        int push_flow = 200000; // INT_MAX funkade inte :(
 
+        // Gå igenom eld och vatten
         Edge *e = parent[sink->id];
         Node *curr = sink;
         while(curr != source)
@@ -97,6 +110,9 @@ int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
             e->flow += push_flow;
 
             curr = other_node(e, curr);
+
+            curr->flow += push_flow;
+
             e = parent[curr->id];
         }
 
@@ -111,7 +127,7 @@ int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
 int main()
 {
     int num_machines, num_wires;
-
+    int test = 1;
     while(cin >> num_machines >> num_wires)
     {
         if(!num_machines && !num_wires)
@@ -120,6 +136,8 @@ int main()
         }
 
         vector<Node> machines(num_machines);
+        vector<Edge*> edges(num_wires);
+
         machines[0].id = 1;
         machines[0].capacity = 200000;
         machines[num_machines-1].id = num_machines;
@@ -137,6 +155,7 @@ int main()
         {
             int node1, node2, capacity;
             cin >> node1 >> node2 >> capacity;
+
             node1--;
             node2--;
 
@@ -144,19 +163,33 @@ int main()
             Node *n2 = &machines[node2];
 
             new Edge(n1, n2, capacity);
+            //edges[i] = new Edge(n1, n2, capacity);
         }
 
 
         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 << "  Cap: " << n->capacity << "   Flow: " << n->flow << 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;
+//            }
+//        }
+
+//        if(test++ == 58000)
+//        {
+//            cout << num_machines << " " << num_wires << endl;
+//            for(auto m : machines)
+//            {
+//                cout << m.id << " " << m.capacity << endl;
+//            }
+//            for(auto e : edges)
+//            {
+//                cout << e->n1->id << " " << e->n2->id << " " << e->capacity << endl;
+//            }
+//        }
 
     }