BotByte

Flow.cpp

Sep 5th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. //#pragma comment(linker, "/STACK:16777216")
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <fstream>
  5. #include <iostream>
  6. #include <string>
  7. #include <complex>
  8. #include <math.h>
  9. #include <set>
  10. #include <vector>
  11. #include <map>
  12. #include <queue>
  13. #include <stdio.h>
  14. #include <stack>
  15. #include <algorithm>
  16. #include <list>
  17. #include <ctime>
  18. #include <memory.h>
  19. #include <assert.h>
  20.  
  21. #define y0 sdkfaslhagaklsldk
  22. #define y1 aasdfasdfasdf
  23. #define yn askfhwqriuperikldjk
  24. #define j1 assdgsdgasghsf
  25. #define tm sdfjahlfasfh
  26. #define lr asgasgash
  27. #define norm asdfasdgasdgsd
  28.  
  29. #define eps 1e-7
  30. #define M_PI 3.141592653589793
  31. #define bs 1000000007
  32. #define bsize 512
  33.  
  34. const int N = 200500;
  35.  
  36. using namespace std;
  37.  
  38. struct edge
  39. {
  40.     int a, b, cap, flow;
  41. };
  42.  
  43. int n, s, t, d[N], ptr[N];
  44. vector<edge> e;
  45. vector<int> g[N];
  46.  
  47. void add_edge(int a, int b, int cap)
  48. {
  49.     edge e1 = { a, b, cap, 0 };
  50.     edge e2 = { b, a, 0, 0 };
  51.     g[a].push_back(e.size());
  52.     e.push_back(e1);
  53.     g[b].push_back(e.size());
  54.     e.push_back(e2);
  55. }
  56.  
  57. bool bfs()
  58. {
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         d[i] = -1;
  62.     }
  63.     d[s] = 0;
  64.     queue<int> qu;
  65.     qu.push(s);
  66.     while (qu.size())
  67.     {
  68.         int v = qu.front();
  69.         qu.pop();
  70.         for (int i = 0; i < g[v].size(); i++)
  71.         {
  72.             int id = g[v][i];
  73.             int to = e[id].b;
  74.             if (d[to] == -1 && e[id].flow < e[id].cap)
  75.             {
  76.                 d[to] = d[v] + 1;
  77.                 qu.push(to);
  78.             }
  79.         }
  80.     }
  81.     return d[t] != -1;
  82. }
  83.  
  84. int dfs(int v, int flow)
  85. {
  86.     if (v == t || flow == 0)
  87.         return flow;
  88.     for (; ptr[v] < g[v].size(); ptr[v]++)
  89.     {
  90.         int id = g[v][ptr[v]];
  91.         int to = e[id].b;
  92.         if (d[to] != d[v] + 1)
  93.             continue;
  94.         int pushed = dfs(to, min(flow, e[id].cap - e[id].flow));
  95.         if (pushed)
  96.         {
  97.             e[id].flow += pushed;
  98.             e[id ^ 1].flow -= pushed;
  99.             return pushed;
  100.         }
  101.     }
  102.     return 0;
  103. }
  104.  
  105. int dinic()
  106. {
  107.     int flow = 0;
  108.     while (true)
  109.     {
  110.         if (!bfs())
  111.             break;
  112.         for (int i = 0; i < n; i++)
  113.         {
  114.             ptr[i] = 0;
  115.         }
  116.         while (true)
  117.         {
  118.             int pushed = dfs(s, 10000);
  119.             if (!pushed)
  120.                 break;
  121.             flow += pushed;
  122.         }
  123.     }
  124.     return flow;
  125. }
  126.  
  127. int m, x, k;
  128. vector<pair<pair<int, int>, int> > E;
  129.  
  130. void construct_graph(double val)
  131. {
  132.     e.clear();
  133.     for (int i = 0; i < n; i++)
  134.         g[i].clear();
  135.     for (int i = 0; i < E.size(); i++)
  136.     {
  137.         //cout << E[i].second << " " << val << endl;
  138.         long double d_here = E[i].second*1.0 / val+eps;
  139.         if (d_here>1e9)
  140.             d_here = 1e6;
  141.         int here = d_here;
  142.         add_edge(E[i].first.first, E[i].first.second, here);
  143.     }
  144. }
  145.  
  146. void update_graph(double val)
  147. {
  148.     for (int i = 0; i < E.size(); i++)
  149.     {
  150.         double d_here = E[i].second*1.0 / val+eps;
  151.         if (d_here>1e9)
  152.             d_here = 1e6;
  153.         int here = d_here;
  154.         int rem = here - e[i * 2].cap;
  155.         e[i * 2].cap += rem;
  156.         //cout << rem << " " << val << " " << e[i * 2].cap << " "<<e[i*2].flow<<endl;
  157.     }
  158. }
  159.  
  160. double get_next(double val)
  161. {
  162.     double res = 0;
  163.     for (int i = 0; i < E.size(); i++)
  164.     {
  165.         double d_here = E[i].second*1.0 / val+eps;
  166.         if (d_here>1e9)
  167.             d_here = 1e6;
  168.         int here = d_here;
  169.         double thold = (E[i].second*1.0 / (here + 1));
  170.         res = max(res, thold);
  171.     }
  172.     return res;
  173. }
  174.  
  175. vector<pair<int, double> > G[N];
  176. double D[N];
  177. //set<pair<double, int> > S;
  178. //set<pair<double, int> > ::iterator it;
  179.  
  180. priority_queue<pair<double, int> > S,emp;
  181.  
  182. double smart_next(double val)
  183. {
  184.     for (int i = 0; i < n; i++)
  185.     {
  186.         G[i].clear();
  187.         D[i] = 0;
  188.     }
  189.  
  190.     for (int i = 0; i < E.size(); i++)
  191.     {
  192.         //cout << e[i*2].a<<" "<<e[i*2].b<<" "<<e[i * 2].cap << " " << e[i * 2].flow << endl;
  193.         double T = E[i].second*1.0 / (e[i * 2].cap + 1);
  194.         if (e[i * 2].flow < e[i * 2].cap)
  195.             G[E[i].first.first].push_back(make_pair(E[i].first.second, val));
  196.         else
  197.             G[E[i].first.first].push_back(make_pair(E[i].first.second, T));
  198.         if (e[i * 2 + 1].flow < e[i * 2+1].cap)
  199.         {
  200.             G[E[i].first.second].push_back(make_pair(E[i].first.first, val));
  201.         }
  202.         else
  203.             G[E[i].first.second].push_back(make_pair(E[i].first.first, T));
  204.     }
  205.  
  206.     S = emp;
  207.  
  208.     D[0] = val;
  209.     for (int i = 0; i < n; i++)
  210.         S.push(make_pair(D[i], i));
  211.  
  212.     while (S.size())
  213.     {
  214.         pair<double, int> p = S.top();
  215.         S.pop();
  216.         if (D[p.second]>p.first + eps)
  217.             continue;
  218.         int cur = p.second;
  219.         if (cur == n - 1)
  220.             break;
  221.         //cout << cur << " " << p.first <<" "<<S.size()<< endl;
  222.         for (int i = 0; i < G[cur].size(); i++)
  223.         {
  224.             int to = G[cur][i].first;
  225.             double cost = min(G[cur][i].second, D[cur]);
  226.             if (D[to]<cost-eps)
  227.             {
  228.                 //S.erase(make_pair(D[to], to));
  229.                 D[to] = cost;
  230.                 S.push(make_pair(D[to], to));
  231.             }
  232.         }
  233.     }
  234.     return D[n - 1];
  235. }
  236.  
  237. double solve_naive(int need)
  238. {
  239.     long double l, r;
  240.     l = 1e-6;
  241.     r = 1e6;
  242.     for (int iter = 1; iter <= 70; iter++)
  243.     {
  244.         double mid = l + r;
  245.         mid /= 2;
  246.         construct_graph(mid);
  247.         int here = dinic();
  248.         if (here >= need)
  249.             l = mid;
  250.         else
  251.             r = mid;
  252.     }
  253.     return l;
  254. }
  255.  
  256. int main(){
  257.     //freopen("route.in","r",stdin);
  258.     //freopen("route.out","w",stdout);
  259.     //freopen("F:/in.txt", "r", stdin);
  260.     //freopen("F:/output.txt", "w", stdout);
  261.     ios_base::sync_with_stdio(0);
  262.     //cin.tie(0);
  263.    
  264.     cin >> n >> m >> x;
  265.     k = 1;
  266.  
  267.     s = 0;
  268.     t = n - 1;
  269.     for (int i = 1; i <= m; i++)
  270.     {
  271.         int a, b, c;
  272.         cin >> a >> b >> c;
  273.         --a;
  274.         --b;
  275.         E.push_back(make_pair(make_pair(a, b), c));
  276.     }
  277.  
  278.     cout.precision(7);
  279.  
  280.     long double cur_val = solve_naive(x);
  281.  
  282.     construct_graph(cur_val - eps);
  283.     int cur_flow = dinic();
  284.  
  285.     for (int i = x; i < x + k; i++)
  286.     {
  287.         //cout << i << "@" << cur_flow << endl;
  288.         while (cur_flow < i)
  289.         {
  290.             double next_cand = smart_next(cur_val);
  291.             update_graph(next_cand);
  292.             //cout << next_cand << endl;
  293.             //cin.get();
  294.             bfs();
  295.             for (int j = 0; j < n; j++)
  296.                 ptr[j] = 0;
  297.             while (true)
  298.             {
  299.                 int pushed = dfs(s, 1000000);
  300.                 if (pushed == 0)
  301.                     break;
  302.                 cur_flow += pushed;
  303.             }
  304. //          cout << pushed << endl;
  305. //          cin.get();
  306.             cur_val = next_cand;
  307.         }
  308.         cout << fixed << cur_val*i << "\n";
  309.     }
  310.     /*
  311.     for (int i = x; i < x + k; i++)
  312.     {
  313.         cout << fixed << solve_naive(i)*i << endl;
  314.     }*/
  315.  
  316.     cin.get(); cin.get();
  317.     return 0;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment