YauhenMardan

Untitled

Apr 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3.  
  4. struct Arc{
  5. int v,c,u,f;
  6. size_t back;
  7. };
  8.  
  9. class Graph{
  10. private:
  11. std::vector<std::vector<Arc>> adjList;
  12. int n;
  13. int m;
  14. int s;
  15. int t;
  16.  
  17. int flow;
  18. int cost;
  19.  
  20. void addOriented(int a,int b,int u,int c){
  21. Arc r1 = { b, u, c, 0, adjList[b].size() };
  22. Arc r2 = { a, 0, -c, 0, adjList[a].size() };
  23. adjList[a].push_back (r1);
  24. adjList[b].push_back (r2);
  25. }
  26. public:
  27. Graph(int n,int m,int s,int t){
  28. adjList.resize(n);
  29. this->n=n;
  30. this->m=m;
  31. this->s=s;
  32. this->t=t;
  33. flow=0;
  34. cost=0;
  35. }
  36. void add(int a,int b,int u,int c){
  37. addOriented(a, b, u, c);
  38. addOriented(b, a, u, c);
  39. }
  40. int getCost(){
  41. return cost;
  42. }
  43. int getFlow(){
  44. return flow;
  45. }
  46. void countCostAndFlow(){
  47.  
  48. int k=INT_MAX;
  49.  
  50. while (flow < k) {
  51. std::vector<int> id (n, 0);
  52. std::vector<int> d (n, INT_MAX);
  53. std::vector<int> q (n);
  54. std::vector<int> p (n);
  55. std::vector<size_t> p_rib (n);
  56. int qh=0, qt=0;
  57. q[qt++] = s;
  58. d[s] = 0;
  59. while (qh != qt) {
  60. int v = q[qh++];
  61. id[v] = 2;
  62. if (qh == n) qh = 0;
  63. for (size_t i=0; i<adjList[v].size(); ++i) {
  64. Arc & r = adjList[v][i];
  65. if (r.f < r.u && d[v] + r.c < d[r.v]) {
  66. d[r.v] = d[v] + r.c;
  67. if (id[r.v] == 0) {
  68. q[qt++] = r.v;
  69. if (qt == n) qt = 0;
  70. }
  71. else if (id[r.v] == 2) {
  72. if (--qh == -1) qh = n-1;
  73. q[qh] = r.v;
  74. }
  75. id[r.v] = 1;
  76. p[r.v] = v;
  77. p_rib[r.v] = i;
  78. }
  79. }
  80. }
  81. if (d[t] == INT_MAX) break;
  82. int addflow = k - flow;
  83. for (int v=t; v!=s; v=p[v]) {
  84. int pv = p[v]; size_t pr = p_rib[v];
  85. addflow = min (addflow, adjList[pv][pr].u - adjList[pv][pr].f);
  86. }
  87. for (int v=t; v!=s; v=p[v]) {
  88. int pv = p[v]; size_t pr = p_rib[v], r = adjList[pv][pr].back;
  89. adjList[pv][pr].f += addflow;
  90. adjList[v][r].f -= addflow;
  91. cost += adjList[pv][pr].c * addflow;
  92. }
  93. flow += addflow;
  94. }
  95. }
  96. int min(int a,int b){
  97. return a<b ? a : b;
  98. }
  99. };
  100.  
  101. int main(int argc, const char * argv[]) {
  102. std::ifstream fin("input.txt");
  103. std::ofstream fout("output.txt");
  104.  
  105. int n,m,s,t;
  106. fin>>n>>m>>s>>t;
  107.  
  108. s--;
  109. t--;
  110.  
  111. Graph gr(n,m,s,t);
  112.  
  113. int a,b,u,c;
  114.  
  115. for(int i=0;i<m;i++){
  116. fin>>a>>b>>u>>c;
  117. gr.add(a-1, b-1, u, c);
  118. }
  119.  
  120. gr.countCostAndFlow();
  121.  
  122. fout<<gr.getFlow()<<"\n"<<gr.getCost()<<"\n";
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment