p11506.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <iostream>
  2. #include <utility>
  3. #include <list>
  4. #include <vector>
  5. #include <cmath>
  6. #include <queue>
  7. using namespace std;
  8. struct Edge;
  9. struct Node
  10. {
  11. Node():flow{0} {}
  12. Node(unsigned int size):flow{0}, neighbours{size} {}
  13. int id;
  14. int capacity;
  15. int flow;
  16. vector<struct Edge*> neighbours;
  17. };
  18. struct Edge
  19. {
  20. Edge(Node *n1, Node *n2, int cap):n1{n1}, n2{n2}, capacity{cap}, flow{0}
  21. {
  22. //capacity = min(min(n1->capacity, n2->capacity), cap);
  23. n1->neighbours.push_back(this);
  24. n2->neighbours.push_back(this);
  25. }
  26. Node *n1, *n2;
  27. int capacity;
  28. int flow;
  29. };
  30. Node *other_node(Edge *e, Node *curr)
  31. {
  32. if(e->n1 == curr)
  33. return e->n2;
  34. return e->n1;
  35. }
  36. int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
  37. {
  38. // Edmonds karp-variant
  39. int max_flow = 0;
  40. // Lägg in en båge från source till sig själv för att undvika specialfall
  41. Edge *es = new Edge(source, source, 200000);
  42. // Medan vi inte har hittat alla intressanta vägar från source till sink
  43. while(true)
  44. {
  45. // Förbered vektor att spara alla bågar vi gått till respektive nod
  46. vector<Edge*> parent(m.size()+1, nullptr);
  47. // Kö för bredden först
  48. queue<Node*> q;
  49. q.push(source);
  50. parent[source->id] = es; // Lägg in special-bågen för source
  51. // Bredden först typ
  52. while(!q.empty())
  53. {
  54. Node *curr = q.front();
  55. q.pop();
  56. for(auto e : curr->neighbours)
  57. {
  58. // Pekare till noden o på andra sidan bågen e
  59. Node *o = other_node(e, curr);
  60. if(parent[o->id] == nullptr && e->capacity > e->flow && o->capacity > o->flow)
  61. {
  62. // Ifall vi inte gått vägen innan, och den fortfarande har kapacitet kvar
  63. // Lägg till noden till bredden först
  64. q.push(o);
  65. // och spara bågen vi tog för att hamna i nuvarande nod
  66. parent[o->id] = e;
  67. }
  68. }
  69. }
  70. // No (new) way to sink
  71. if(parent[sink->id] == nullptr)
  72. break;
  73. int push_flow = 200000; // INT_MAX funkade inte :(
  74. // Gå igenom eld och vatten
  75. Edge *e = parent[sink->id];
  76. Node *curr = sink;
  77. while(curr != source)
  78. {
  79. push_flow = min(push_flow, (e->capacity - e->flow) );
  80. curr = other_node(e, curr);
  81. push_flow = min(push_flow, (curr->capacity - curr->flow) );
  82. e = parent[curr->id];
  83. }
  84. e = parent[sink->id];
  85. curr = sink;
  86. while(curr != source)
  87. {
  88. e->flow += push_flow;
  89. curr = other_node(e, curr);
  90. curr->flow += push_flow;
  91. e = parent[curr->id];
  92. }
  93. max_flow += push_flow;
  94. }
  95. delete es;
  96. return max_flow;
  97. }
  98. int main()
  99. {
  100. int num_machines, num_wires;
  101. int test = 1;
  102. while(cin >> num_machines >> num_wires)
  103. {
  104. if(!num_machines && !num_wires)
  105. {
  106. break;
  107. }
  108. vector<Node> machines(num_machines);
  109. vector<Edge*> edges(num_wires);
  110. machines[0].id = 1;
  111. machines[0].capacity = 200000;
  112. machines[num_machines-1].id = num_machines;
  113. machines[num_machines-1].capacity = 200000;
  114. for(int i = 0; i < num_machines-2; i++)
  115. {
  116. int id, capacity;
  117. cin >> id >> capacity;
  118. machines.at(id-1).id = id;
  119. machines.at(id-1).capacity = capacity;
  120. }
  121. for(int i = 0; i < num_wires; i++)
  122. {
  123. int node1, node2, capacity;
  124. cin >> node1 >> node2 >> capacity;
  125. node1--;
  126. node2--;
  127. Node *n1 = &machines[node1];
  128. Node *n2 = &machines[node2];
  129. new Edge(n1, n2, capacity);
  130. //edges[i] = new Edge(n1, n2, capacity);
  131. }
  132. cout << edmonds_karp(machines, &machines[0], &machines[num_machines-1]) << endl;
  133. // for(auto m : machines)
  134. // {
  135. // cout << m.id << ":" << m.capacity << endl;
  136. // for(auto n : m.neighbours)
  137. // {
  138. // cout << "\t" << n->n1->id << " <--> " << n->n2->id << " Cap: " << n->capacity << " Flow: " << n->flow << endl;
  139. // }
  140. // }
  141. // if(test++ == 58000)
  142. // {
  143. // cout << num_machines << " " << num_wires << endl;
  144. // for(auto m : machines)
  145. // {
  146. // cout << m.id << " " << m.capacity << endl;
  147. // }
  148. // for(auto e : edges)
  149. // {
  150. // cout << e->n1->id << " " << e->n2->id << " " << e->capacity << endl;
  151. // }
  152. // }
  153. }
  154. return 0;
  155. }