mr_dot_convict

11284-Shopping-Trip-UVa-mr.convict

Jun 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (2); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ] "
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20. using ld = long double;
  21. using pii = pair<int,int>;
  22.  
  23. #define debug(args...) { \
  24.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  25.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  26. }
  27. void err(istream_iterator<string> it) { it->empty();
  28.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  29. }
  30. template<typename T, typename... Args>
  31. void err(istream_iterator<string> it, T a, Args... args) {
  32.     cerr << fixed << setprecision(10) << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  33.     err(++it, args...);
  34. }
  35.  
  36. const int N = 13, M = 55;
  37. const ld inf = 1e18, eps = 1e-9;
  38. ld dp[N][1 << N], wgt[N][N], W[M][M];
  39. int par[N][1 << N], start[N][1 << N], cd[N];
  40. ld price[N];
  41. int n, nc, m;
  42.  
  43. ld tsp() {
  44.    for (int mask = 0; mask < (1 << n); ++mask)
  45.       for (int i = 0; i < n; ++i)
  46.          dp[i][mask] = inf;
  47.  
  48.    for (int i = 0; i < n; ++i)
  49.       dp[i][1 << i] = 0, par[i][1 << i] = -1, start[i][1 << i] = i;
  50.  
  51.    for (int mask = 1; mask < (1 << n); ++mask) {
  52.       for (int pos = 0; pos < n; ++pos)
  53.       {
  54.          if (!(mask & (1 << pos))) continue;
  55.          ld mn_val = inf;
  56.          for (int prv = 0; prv < n; ++prv) {
  57.             if (pos == prv || !(mask & (1 << prv)))
  58.                continue;
  59.             ld guess = dp[prv][mask-(1<<pos)] + wgt[prv][pos];
  60.             if (mn_val + eps > guess) {
  61.                mn_val = guess;
  62.                par[pos][mask] = prv;
  63.                start[pos][mask] = start[prv][mask-(1 << pos)];
  64.             }
  65.          }
  66.          dp[pos][mask] = min(dp[pos][mask], mn_val);
  67.       }
  68.    }
  69.  
  70.    ld mn_cycle = inf;
  71.    int pos = -1, mask = (1 << n) - 1;
  72.    for (int i = 0; i < n; ++i) {
  73.       ld go = dp[i][mask] + wgt[i][start[i][mask]];
  74.       if (mn_cycle + eps > go) {
  75.          mn_cycle = go;
  76.          pos = i;
  77.       }
  78.    }
  79.  
  80.    vector <int> cycle;
  81.    while (pos != -1) {
  82.       cycle.push_back(pos);
  83.       int tmp = pos;
  84.       pos = par[pos][mask];
  85.       mask -= (1 << tmp);
  86.    }
  87.    // for (int v : cycle) { cerr << v << ' '; } cerr << '\n';
  88.  
  89.    return mn_cycle;
  90. }
  91.  
  92. void solve() {
  93.    for (int k = 0; k < nc; ++k)
  94.       for (int i = 0; i < nc; ++i) for (int j = 0; j < nc; ++j)
  95.             W[i][j] = W[j][i] = min(W[i][j], W[i][k] + W[k][j]);
  96.  
  97.  
  98.    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j)
  99.       wgt[i][j] = W[cd[i]][cd[j]];
  100.  
  101.    ld total_tour_cost = tsp();
  102.    ld mx_saving = 0;
  103.  
  104.    for (int mask = 1; mask < (1 << n); ++mask) {
  105.       if (!(mask & 1)) continue; // should contain home
  106.  
  107.       ld min_tour_cost = inf;
  108.       ld saving = 0;
  109.  
  110.       for (int i = 0; i < n; ++i) {
  111.          if (mask & (1 << i)) {
  112.             saving += price[i];
  113.  
  114.             ld go = dp[i][mask] + wgt[i][start[i][mask]];
  115.             min_tour_cost = min(min_tour_cost, go);
  116.          }
  117.       }
  118.       if (saving + eps > min_tour_cost)
  119.          mx_saving = max(mx_saving, saving - min_tour_cost);
  120.    }
  121.  
  122.    if (fabs(mx_saving - 0) < eps) {
  123.       cout << "Don't leave the house\n";
  124.    }
  125.    else {
  126.       cout << "Daniel can save $" << mx_saving << '\n';
  127.    }
  128. }
  129.  
  130. void read() {
  131.    cin >> nc >> m;
  132.    ++nc;
  133.    for (int i = 0; i < nc; ++i) for (int j = 0; j < nc; ++j)
  134.       W[i][j] = inf;
  135.  
  136.    for (int e = 0; e < m; ++e) {
  137.       int u, v;
  138.       ld wt;
  139.       cin >> u >> v >> wt;
  140.       W[u][v] = W[v][u] = min(W[u][v], wt);
  141.    }
  142.  
  143.    cin >> n;
  144.    ++n;
  145.  
  146.    cd[0] = 0, price[0] = 0;
  147.    for (int i = 1; i < n; ++i) {
  148.       cin >> cd[i] >> price[i];
  149.    }
  150. }
  151.  
  152. signed main() {
  153.    IOS; PREC;
  154.    int tc; cin >> tc;
  155.    while (tc--) read(), solve();
  156.  
  157.    return EXIT_SUCCESS;
  158. }
Add Comment
Please, Sign In to add comment