Advertisement
Guest User

Untitled

a guest
May 20th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 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* arc : res) {
  54. arc->cur_thread += count_thread;
  55. arc->obr_vert->cur_thread -= count_thread;
  56. }
  57. return count_thread;
  58. }
  59. for (int i = 0; i < graph[v].size(); i++) {
  60. arc* arc = graph[v][i];
  61. if (!path[arc->v] && arc->c - arc->cur_thread > 0) {
  62. path[arc->v] = arc;
  63. q.push(arc->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* arc;
  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. arc = graph[i][j];
  87. if (d[arc->v] > d[arc->u] + arc->cost && arc->c - arc->cur_thread > 0) {
  88. d[arc->v] = max(d[arc->u] + arc->cost, -(long long)MAX_VAL);
  89. path[arc->v] = arc;
  90. flow = arc->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* arc : res) {
  146. arc->cur_thread += count_thread;
  147. arc->obr_vert->cur_thread -= count_thread;
  148. }
  149. return count_thread;
  150. }
  151. for (int i = 0; i < newgraph[v].size(); i++) {
  152. arc* arc = newgraph[v][i];
  153. if (!path[arc->v] && arc->c - arc->cur_thread > 0) {
  154. path[arc->v] = arc;
  155. q.push(arc->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* arc;
  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. arc = newgraph[i][j];
  179. if (d[arc->v] > d[arc->u] + arc->cost && arc->c - arc->cur_thread > 0) {
  180. d[arc->v] = max(d[arc->u] + arc->cost, -(long long)MAX_VAL);
  181. path[arc->v] = arc;
  182. flow = arc->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)
  195. break;
  196. }
  197. if (res.size() > 0)
  198. res.pop_back();
  199. for (int i = 0; i < res.size(); i++)
  200. add = min(add, res[i]->c - res[i]->cur_thread);
  201. for (int i = 0; i < res.size(); i++) {
  202. res[i]->cur_thread += add;
  203. res[i]->obr_vert->cur_thread -= add;
  204. }
  205. return true;
  206. }
  207. return false;
  208. }
  209.  
  210. void no_nevative_cycle_newgraph() {
  211. while (negative_cycle_newgraph()) {}
  212. }
  213.  
  214. void min_cost_flow_newgraph() {
  215. no_nevative_cycle_newgraph();
  216. for (int i = 0; i < n; i++)
  217. for (int j = 0; j < newgraph[i].size(); j++)
  218. if (newgraph[i][j]->cur_thread > 0)
  219. min_cost_newgraph += newgraph[i][j]->cost * newgraph[i][j]->cur_thread;
  220. }
  221.  
  222. int main() {
  223. FILE* in = fopen("input.txt", "rt");
  224. FILE* out = fopen("output.txt", "wt");
  225. fscanf(in, "%d", &n);
  226. fscanf(in, "%d", &m);
  227. fscanf(in, "%d", &s);
  228. fscanf(in, "%d", &t);
  229. s--;
  230. t--;
  231. int x = 0, y = 0, c = 0, cost=0;
  232. for (int i = 0; i < m; i++) {
  233. fscanf(in, "%d %d %d %d", &x, &y, &c, &cost);
  234. arc* x1 = new arc(x - 1, y - 1, c, cost);
  235. arc* x2 = new arc(y - 1, x - 1, 0, -cost);
  236. graph[x - 1].push_back(x1);
  237. graph[y - 1].push_back(x2);
  238. x1->obr_vert = x2;
  239. x2->obr_vert = x1;
  240. }
  241. max_flow_ff_graph();
  242. min_cost_flow_graph();
  243. long long matr[MAX_N][MAX_N];
  244. for (int i = 0; i < n; i++) {
  245. for (int j = 0; j < n; j++) {
  246. matr[i][j] = 0;
  247. }
  248. }
  249. for (int i = 0; i < n; i++) {
  250. for (int j = 0; j < graph[i].size(); j++) {
  251. graph[i][j]->cur_thread = 0;
  252. }
  253. }
  254. while (!feof(in)) {
  255. fscanf(in, "%d %d", &x, &y);
  256. x--;
  257. y--;
  258. matr[x][y] = 1;
  259. matr[y][x] = 1;
  260. }
  261. for (int i = 0; i < n; i++) {
  262. for (int j = 0; j <graph[i].size(); j++)
  263. if (matr[i][graph[i][j]->v] == 1 || matr[graph[i][j]->v][i] == 1) {
  264. newgraph[i].push_back(graph[i][j]);
  265. }
  266. }
  267. max_flow_ff_newgraph();
  268. min_cost_flow_newgraph();
  269. if (min_cost != min_cost_newgraph) {
  270. fprintf(out, "No\n%lld\n%lld\n%lld\n%lld",max_flow,min_cost,max_flow_newgraph,min_cost_newgraph);
  271. }
  272. else {
  273. fprintf(out, "Yes\n%lld\n%lld", max_flow, min_cost);
  274. }
  275. system("pause");
  276. return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement