Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <climits>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8. int n, s, t;
  9. int doo[102], part[102], query[102];
  10. int smegnost[102][102], f[102][102];
  11.  
  12. bool bfs() {
  13. int qh = 0, qt = 0;
  14. query[qt++] = s;
  15. memset(doo, -1, n * sizeof doo[0]);
  16. doo[s] = 0;
  17. while (qh < qt) {
  18. int v = query[qh++];
  19. for (auto i = 0; i < n; ++i)
  20. if (doo[i] == -1 && f[v][i] < smegnost[v][i]){
  21. query[qt++] = i;
  22. doo[i] = doo[v] + 1;
  23. }
  24. }
  25. return doo[t] != -1;
  26. }
  27.  
  28. int dfs (int v, int flow) {
  29. if (!flow) return 0;
  30. if (v == t) return flow;
  31. int x = part[v];
  32. for (auto i = x; i < n; ++i) {
  33. if (doo[i] != doo[v] + 1) continue;
  34. int push = dfs(i, min(flow, smegnost[v][i] - f[v][i]));
  35. if (push) {
  36. f[v][i] += push;
  37. f[i][v] -= push;
  38. return push;
  39. }
  40. }
  41. return 0;
  42. }
  43.  
  44. int main(){
  45. ifstream fin("circulation.in");
  46. ofstream fout("circulation.out");
  47. int m;
  48. fin >> n >> m;
  49. n = n + 1;
  50. for(auto i = 0; i < m; ++i){
  51. int x, y, z, q;
  52. fin >> x >> y >> z >> q;
  53. if(z == 0)
  54. smegnost[x][y] = q;
  55. else{
  56. smegnost[0][y] = z;
  57. smegnost[x][n] = z;
  58. smegnost[x][y] = q - z;
  59. }
  60. }
  61. s = 0; t = n - 1;
  62. int foo = 0;
  63. for (;;) {
  64. if (!bfs()) break;
  65. memset(part, 0, n * sizeof part[0]);
  66. while (int push = dfs(s, INT_MAX)) foo += push;
  67. }
  68. fout << foo;
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement