Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <set>
  6. #include <map>
  7. #include <queue>
  8. #include <cassert>
  9. #define PB push_back
  10. #define MP make_pair
  11. #define sz(v) (ll((v).size()))
  12. #define forn(i,n) for(ll i = 0; i < (n); ++i)
  13. #define forv(i,v) forn(i,sz(v))
  14. #define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i)
  15. #define all(v) (v).begin(), (v).end()
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef pair<ll, ll> pii;
  21. typedef vector<ll> vi;
  22. typedef vector<vi> vvi;
  23.  
  24. const int N = 1000010;
  25. ll m, n, k;
  26.  
  27. map<pair<ll, ll> , double> memo;
  28.  
  29. map<ll, vector< pair<ll, pair<int, double> > > > depart[N];
  30.  
  31. ll next(ll city, ll t) {
  32.     auto ans = depart[city].lower_bound(t + 1);
  33.     return ans == depart[city].end() ? k : ans->first;
  34. }
  35.  
  36. double dp(ll src, ll t) {
  37.     if (memo.find(MP(src, t)) != memo.end()) return memo[MP(src, t)];
  38.     if (t == k) return (src == 1 ? 1.0 : 0.0);
  39.     // iterate all starting at t
  40.     double ans = dp(src, next(src, t));
  41.     double stay = ans;
  42.     for (auto tri : depart[src][t]) {
  43.         auto key = tri.second;
  44.         double take = dp(tri.first, next(tri.first, tri.second.first));
  45.         ans = max(ans, take * tri.second.second + stay * (1 - tri.second.second));
  46.     }
  47.     //cerr << src << " " << t << " " << ans << endl;
  48.     return memo[MP(src, t)] = ans;
  49. }
  50.  
  51.  
  52. int main() {
  53.     ios::sync_with_stdio(0); cin.tie(0);
  54.  
  55.     cin >> m >> n >> k;
  56.     forn(i, m) {
  57.         ll p1, p2, t1, t2; double prob;
  58.         cin >> p1 >> p2 >> t1 >> t2 >> prob;
  59.         depart[p1][t1].reserve(1);
  60.         depart[p1][t1].PB( MP(p2, MP(t2, prob)) );
  61.     }
  62.     cout.precision(10);
  63.     cout << dp(0, next(0, -1)) << endl;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement