p11506.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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}, 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. e = parent[curr->id];
  82. }
  83. e = parent[sink->id];
  84. curr = sink;
  85. while(curr != source)
  86. {
  87. e->flow += push_flow;
  88. curr = other_node(e, curr);
  89. curr->flow += push_flow;
  90. e = parent[curr->id];
  91. }
  92. max_flow += push_flow;
  93. }
  94. delete es;
  95. return max_flow;
  96. }
  97. int main()
  98. {
  99. int num_machines, num_wires;
  100. int test = 1;
  101. while(cin >> num_machines >> num_wires)
  102. {
  103. if(!num_machines && !num_wires)
  104. {
  105. break;
  106. }
  107. vector<Node> machines(num_machines);
  108. vector<Edge*> edges(num_wires);
  109. machines[0].id = 1;
  110. machines[0].capacity = 200000;
  111. machines[num_machines-1].id = num_machines;
  112. machines[num_machines-1].capacity = 200000;
  113. for(int i = 0; i < num_machines-2; i++)
  114. {
  115. int id, capacity;
  116. cin >> id >> capacity;
  117. machines.at(id-1).id = id;
  118. machines.at(id-1).capacity = capacity;
  119. }
  120. for(int i = 0; i < num_wires; i++)
  121. {
  122. int node1, node2, capacity;
  123. cin >> node1 >> node2 >> capacity;
  124. node1--;
  125. node2--;
  126. Node *n1 = &machines[node1];
  127. Node *n2 = &machines[node2];
  128. new Edge(n1, n2, capacity);
  129. //edges[i] = new Edge(n1, n2, capacity);
  130. }
  131. cout << edmonds_karp(machines, &machines[0], &machines[num_machines-1]) << endl;
  132. // for(auto m : machines)
  133. // {
  134. // cout << m.id << ":" << m.capacity << endl;
  135. // for(auto n : m.neighbours)
  136. // {
  137. // cout << "\t" << n->n1->id << " <--> " << n->n2->id << " Cap: " << n->capacity << " Flow: " << n->flow << endl;
  138. // }
  139. // }
  140. // if(test++ == 58000)
  141. // {
  142. // cout << num_machines << " " << num_wires << endl;
  143. // for(auto m : machines)
  144. // {
  145. // cout << m.id << " " << m.capacity << endl;
  146. // }
  147. // for(auto e : edges)
  148. // {
  149. // cout << e->n1->id << " " << e->n2->id << " " << e->capacity << endl;
  150. // }
  151. // }
  152. }
  153. return 0;
  154. }