Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <fstream>
  3. #include <queue>
  4. #include <vector>
  5. #include <algorithm>
  6. #define MAX_VAL 1000000000
  7. #define MAX_N 101
  8. using namespace std;
  9. int n, m,s,t;
  10. long long max_flow=0,min_cost=0,min_cost_newgraph=0,max_flow_newgraph=0;
  11. long long d[MAX_N];
  12. long long p[MAX_N];
  13.  
  14. struct arc {//arc-дуга
  15. int u;
  16. int v;
  17. long long c; //пропускная способность
  18. long long cost;//стоимость единицы пропускной способности
  19. long long cur_thread;//текущий поток
  20. arc* obr_vert;//обратная дуга
  21. arc(int x, int y, long long cc, long long cost_) : u(x), v(y), c(cc), cost(cost_),cur_thread(0) {};
  22. };
  23.  
  24. vector<arc*> res;
  25. arc* path[MAX_N];
  26. vector<arc*> graph[MAX_N],newgraph[MAX_N];
  27.  
  28. void init() {
  29. for (int i = 0; i < n; i++) {
  30. d[i] = 0;
  31. path[i] = 0;
  32. }
  33. }
  34.  
  35. //functions for graph
  36. long long bfs_graph(int s, int t) {
  37. queue<int> q;
  38. arc* path[MAX_N];
  39. for (int i = 0; i < n; i++)
  40. path[i] = 0;
  41. q.push(s);
  42. long long count_thread = MAX_N;
  43. while (!q.empty()) {
  44. int v = q.front();
  45. q.pop();
  46. if (v == t) {
  47. res.clear();
  48. while (v != s) {
  49. res.push_back(path[v]);
  50. count_thread = min(count_thread, path[v]->c - path[v]->cur_thread);
  51. v = path[v]->u;
  52. }
  53. for (arc* vert : res) {
  54. vert->cur_thread += count_thread;
  55. vert->obr_vert->cur_thread -= count_thread;
  56. }
  57. return count_thread;
  58. }
  59. for (int i = 0; i < graph[v].size(); i++) {
  60. arc* vert = graph[v][i];
  61. if (!path[vert->v] && vert->c - vert->cur_thread > 0) {
  62. path[vert->v] = vert;
  63. q.push(vert->v);
  64. }
  65. }
  66. }
  67. return 0;
  68. }
  69.  
  70. void max_flow_ff_graph(){
  71. long long add = bfs_graph(s, t);
  72. while (add){
  73. max_flow += add;
  74. add = bfs_graph(s, t);
  75. }
  76. }
  77.  
  78. bool negative_cycle_graph() {
  79. init();
  80. int flow;
  81. arc* vert;
  82. for (int z = 0; z < n; z++) {
  83. flow = -1;
  84. for (int i = 0; i < n; i++)
  85. for (int j = 0; j < graph[i].size(); j++) {
  86. vert = graph[i][j];
  87. if (d[vert->v] > d[vert->u] + vert->cost && vert->c - vert->cur_thread > 0) {
  88. d[vert->v] = max(d[vert->u] + vert->cost, -(long long)MAX_VAL);
  89. path[vert->v] = vert;
  90. flow = vert->v;
  91. }
  92. }
  93. }
  94. res.clear();
  95. if (flow != -1) {
  96. int y = flow;
  97. long long add = MAX_VAL;
  98. for (int i = 0; i < n; i++) y = path[y]->u;
  99. for (int cur = y; ; cur = path[cur]->u) {
  100. res.push_back(path[cur]);
  101. if (cur == y && res.size() > 1) break;
  102. }
  103. if (res.size() > 0) res.pop_back();
  104. for (int i = 0; i < res.size(); i++)
  105. add = min(add, res[i]->c - res[i]->cur_thread);
  106. for (int i = 0; i < res.size(); i++) {
  107. res[i]->cur_thread += add;
  108. res[i]->obr_vert->cur_thread -= add;
  109. }
  110. return true;
  111. }
  112. return false;
  113. }
  114.  
  115. void no_nevative_cycle_graph() {
  116. while (negative_cycle_graph()) {}
  117. }
  118.  
  119. void min_cost_flow_graph() {
  120. no_nevative_cycle_graph();
  121. for (int i = 0; i < n; i++)
  122. for (int j = 0; j < graph[i].size(); j++)
  123. if (graph[i][j]->cur_thread > 0)
  124. min_cost += graph[i][j]->cost * graph[i][j]->cur_thread;
  125. }
  126.  
  127. //functions for new graph
  128. long long bfs_newgraph(int s, int t) {
  129. queue<int> q;
  130. arc* path[MAX_N];
  131. for (int i = 0; i < n; i++)
  132. path[i] = 0;
  133. q.push(s);
  134. long long count_thread = MAX_N;
  135. while (!q.empty()) {
  136. int v = q.front();
  137. q.pop();
  138. if (v == t) {
  139. res.clear();
  140. while (v != s) {
  141. res.push_back(path[v]);
  142. count_thread = min(count_thread, path[v]->c - path[v]->cur_thread);
  143. v = path[v]->u;
  144. }
  145. for (arc* vert : res) {
  146. vert->cur_thread += count_thread;
  147. vert->obr_vert->cur_thread -= count_thread;
  148. }
  149. return count_thread;
  150. }
  151. for (int i = 0; i < newgraph[v].size(); i++) {
  152. arc* vert = newgraph[v][i];
  153. if (!path[vert->v] && vert->c - vert->cur_thread > 0) {
  154. path[vert->v] = vert;
  155. q.push(vert->v);
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161.  
  162. void max_flow_ff_newgraph() {
  163. long long add = bfs_newgraph(s, t);
  164. while (add) {
  165. max_flow_newgraph += add;
  166. add = bfs_newgraph(s, t);
  167. }
  168. }
  169.  
  170. bool negative_cycle_newgraph() {
  171. init();
  172. int flow;
  173. arc* vert;
  174. for (int z = 0; z < n; z++) {
  175. flow = -1;
  176. for (int i = 0; i < n; i++)
  177. for (int j = 0; j < newgraph[i].size(); j++) {
  178. vert = newgraph[i][j];
  179. if (d[vert->v] > d[vert->u] + vert->cost && vert->c - vert->cur_thread > 0) {
  180. d[vert->v] = max(d[vert->u] + vert->cost, -(long long)MAX_VAL);
  181. path[vert->v] = vert;
  182. flow = vert->v;
  183. }
  184. }
  185. }
  186. res.clear();
  187. if (flow != -1) {
  188. int y = flow;
  189. long long add = MAX_VAL;
  190. for (int i = 0; i < n; i++)
  191. y = path[y]->u;
  192. for (int cur = y; ; cur = path[cur]->u) {
  193. res.push_back(path[cur]);
  194. if (cur == y && res.size() > 1) break;
  195. }
  196. if (res.size() > 0)
  197. res.pop_back();
  198. for (int i = 0; i < res.size(); i++)
  199. add = min(add, res[i]->c - res[i]->cur_thread);
  200. for (int i = 0; i < res.size(); i++) {
  201. res[i]->cur_thread += add;
  202. res[i]->obr_vert->cur_thread -= add;
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208.  
  209. void no_nevative_cycle_newgraph() {
  210. while (negative_cycle_newgraph()) {}
  211. }
  212.  
  213. void min_cost_flow_newgraph() {
  214. no_nevative_cycle_newgraph();
  215. for (int i = 0; i < n; i++)
  216. for (int j = 0; j < newgraph[i].size(); j++)
  217. if (newgraph[i][j]->cur_thread > 0)
  218. min_cost_newgraph += newgraph[i][j]->cost * newgraph[i][j]->cur_thread;
  219. }
  220.  
  221. int main() {
  222. FILE* in = fopen("input.txt", "rt");
  223. FILE* out = fopen("output.txt", "wt");
  224. fscanf(in, "%d", &n);
  225. fscanf(in, "%d", &m);
  226. fscanf(in, "%d", &s);
  227. fscanf(in, "%d", &t);
  228. s--;
  229. t--;
  230. int x = 0, y = 0, c = 0, cost=0;
  231. for (int i = 0; i < m; i++) {
  232. fscanf(in, "%d %d %d %d", &x, &y, &c, &cost);
  233. arc* x1 = new arc(x - 1, y - 1, c, cost);
  234. arc* x2 = new arc(y - 1, x - 1, 0, -cost);
  235. graph[x - 1].push_back(x1);
  236. graph[y - 1].push_back(x2);
  237. x1->obr_vert = x2;
  238. x2->obr_vert = x1;
  239. }
  240. max_flow_ff_graph();
  241. min_cost_flow_graph();
  242. long long matr[MAX_N][MAX_N];
  243. for (int i = 0; i < n; i++) {
  244. for (int j = 0; j < n; j++) {
  245. matr[i][j] = 0;
  246. }
  247. }
  248. for (int i = 0; i < n; i++) {
  249. for (int j = 0; j < graph[i].size(); j++) {
  250. graph[i][j]->cur_thread = 0;
  251. }
  252. }
  253. while (!feof(in)) {
  254. fscanf(in, "%d %d", &x, &y);
  255. x--;
  256. y--;
  257. matr[x][y] = 1;
  258. matr[y][x] = 1;
  259. }
  260. for (int i = 0; i < n; i++) {
  261. for (int j = 0; j <graph[i].size(); j++)
  262. if (matr[i][graph[i][j]->v] == 1 || matr[graph[i][j]->v][i] == 1) {
  263. newgraph[i].push_back(graph[i][j]);
  264. }
  265. }
  266. max_flow_ff_newgraph();
  267. min_cost_flow_newgraph();
  268. if (min_cost != min_cost_newgraph) {
  269. fprintf(out, "No\n%lld\n%lld\n%lld\n%lld",max_flow,min_cost,max_flow_newgraph,min_cost_newgraph);
  270. }
  271. else {
  272. fprintf(out, "Yes\n%lld\n%lld", max_flow, min_cost);
  273. }
  274. system("pause");
  275. return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement