mr_dot_convict

12179-Randomly-Priced-Ticket-UVa-mr.convict

May 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 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. #pragma GCC      optimize ("Ofast")
  10. #pragma GCC      optimize ("unroll-loops")
  11. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  12.  
  13. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC     cout.precision (6); cout << fixed
  15. #define x        first
  16. #define y        second
  17. using namespace std;
  18.  
  19. const int N = 105;
  20. const int MAXR = 10010;
  21. const int infi = (int)1e9;
  22.  
  23. int n, R, q;
  24. int W[N][N];
  25. long double P[N][MAXR];
  26.  
  27. void floydWarshall () {
  28.    for (int k = 0; k < n; ++k)
  29.       for (int i = 0; i < n; ++i)
  30.          for (int j = 0; j < n; ++j)
  31.             if (W[i][k] < infi && W[k][j] < infi)
  32.                W[j][i] = W[i][j] = min (W[i][j], W[i][k] + W[k][j]);
  33. }
  34.  
  35. void calcProbDP() {
  36.    long double invR = 1.0 / R;
  37.  
  38.    for (int i = 0; i <= n; ++i)
  39.       for(int j = 0; j <= 100 * R; ++j)
  40.          P[i][j] = 0;
  41.  
  42.    for (int i = 0; i <= 100 * R; ++i)
  43.       P[0][i] = 0;
  44.  
  45.    P[0][0] = 1.0;
  46.  
  47.    for (int i = 1; i <= n; ++i)
  48.       for (int Y = 1; Y <= R; ++Y)
  49.          for (int X = Y; X <= 100 * R; ++X)
  50.             P[i][X] += P[i - 1][X - Y] * invR;
  51. }
  52.  
  53. void solve(int tc) {
  54.    floydWarshall();
  55.    calcProbDP();
  56.  
  57.    cin >> q;
  58.    int u, v, budget;
  59.    cout << "Case " << tc << '\n';
  60.    while (q--) {
  61.       cin >> u >> v >> budget;
  62.       --u, --v;
  63.       int d = W[u][v];
  64.  
  65.       long double totalProb = 0;
  66.  
  67.       for (int i = 1; i <= budget; ++i) {
  68.          totalProb += P[d][i];
  69.       }
  70.       assert(totalProb < 1.0 + 1e-9);
  71.  
  72.       cout << totalProb << '\n';
  73.    }
  74. }
  75.  
  76. void read() {
  77.    cin >> n >> R;
  78.    for (int i = 0; i < n; ++i) {
  79.       string s;
  80.       cin >> s;
  81.       assert ((int)s.size() == n);
  82.       for (int j = 0; j < n; ++j) {
  83.          if (s[j] == 'N') W[i][j] = W[j][i] = infi;
  84.          else if (s[j] == 'Y') W[i][j] = W[j][i] = 1;
  85.          else assert (false);
  86.       }
  87.    }
  88. }
  89.  
  90. signed main() {
  91.    IOS; PREC;
  92.    int tc;
  93.    cin >> tc;
  94.    for (int i = 0; i < tc; ) {
  95.       if (i) cout << '\n';
  96.       ++i;
  97.       read();
  98.       solve(i);
  99.    }
  100.    return EXIT_SUCCESS;
  101. }
Add Comment
Please, Sign In to add comment