p11506.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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(){}
  12. Node(unsigned int size):neighbours{size} {}
  13. int id;
  14. int capacity;
  15. vector<struct Edge*> neighbours;
  16. };
  17. struct Edge
  18. {
  19. Edge(Node *n1, Node *n2, int cap):n1{n1}, n2{n2}, flow{0}
  20. {
  21. capacity = min(min(n1->capacity, n2->capacity), cap);
  22. n1->neighbours.push_back(this);
  23. n2->neighbours.push_back(this);
  24. }
  25. Node *n1, *n2;
  26. int capacity;
  27. int flow;
  28. };
  29. Node *other_node(Edge *e, Node *curr)
  30. {
  31. if(e->n1 == curr)
  32. return e->n2;
  33. return e->n1;
  34. }
  35. int edmonds_karp(vector<Node> &m, Node *source, Node *sink)
  36. {
  37. int max_flow = 0;
  38. // Göra bra krejjer
  39. Edge *es = new Edge(source, source, 200000);
  40. while(true)
  41. {
  42. vector<Edge*> parent(m.size()+1, nullptr);
  43. queue<Node*> q;
  44. q.push(source);
  45. parent[source->id] = es;
  46. while(!q.empty())
  47. {
  48. Node *curr = q.front();
  49. q.pop();
  50. for(auto e : curr->neighbours)
  51. {
  52. Node *o = other_node(e, curr);
  53. if(parent[o->id] == nullptr && e->capacity > e->flow)
  54. {
  55. q.push(o);
  56. parent[o->id] = e;
  57. }
  58. }
  59. }
  60. // No (new) way to sink
  61. if(parent[sink->id] == nullptr)
  62. break;
  63. int push_flow = 200000;
  64. Edge *e = parent[sink->id];
  65. Node *curr = sink;
  66. while(curr != source)
  67. {
  68. push_flow = min(push_flow, (e->capacity - e->flow) );
  69. curr = other_node(e, curr);
  70. e = parent[curr->id];
  71. }
  72. e = parent[sink->id];
  73. curr = sink;
  74. while(curr != source)
  75. {
  76. e->flow += push_flow;
  77. curr = other_node(e, curr);
  78. e = parent[curr->id];
  79. }
  80. max_flow += push_flow;
  81. }
  82. delete es;
  83. return max_flow;
  84. }
  85. int main()
  86. {
  87. int num_machines, num_wires;
  88. while(cin >> num_machines >> num_wires)
  89. {
  90. if(!num_machines && !num_wires)
  91. {
  92. break;
  93. }
  94. vector<Node> machines(num_machines);
  95. machines[0].id = 1;
  96. machines[0].capacity = 200000;
  97. machines[num_machines-1].id = num_machines;
  98. machines[num_machines-1].capacity = 200000;
  99. for(int i = 0; i < num_machines-2; i++)
  100. {
  101. int id, capacity;
  102. cin >> id >> capacity;
  103. machines.at(id-1).id = id;
  104. machines.at(id-1).capacity = capacity;
  105. }
  106. for(int i = 0; i < num_wires; i++)
  107. {
  108. int node1, node2, capacity;
  109. cin >> node1 >> node2 >> capacity;
  110. node1--;
  111. node2--;
  112. Node *n1 = &machines[node1];
  113. Node *n2 = &machines[node2];
  114. new Edge(n1, n2, capacity);
  115. }
  116. cout << edmonds_karp(machines, &machines[0], &machines[num_machines-1]) << endl;
  117. for(auto m : machines)
  118. {
  119. cout << m.id << ":" << m.capacity << endl;
  120. for(auto n : m.neighbours)
  121. {
  122. cout << "\t" << n->n1->id << " <--> " << n->n2->id << " Cap: " << n->capacity << " Flow: " << n->flow << endl;
  123. }
  124. }
  125. }
  126. return 0;
  127. }