mr_dot_convict

1201-Taxi-Cab-Scheme-UVa-mr.convict

Jun 1st, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 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 (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33. const int N = 2 * 510;
  34.  
  35. struct booking{
  36.    int time_min;
  37.    int x_src, y_src, x_dest, y_dest;
  38.    int ride_time_in_min;
  39.  
  40.    booking (int hr, int mn, int x_s, int y_s, int x_d, int y_d) {
  41.       time_min = 60 * hr + mn, x_src = x_s, y_src = y_s, x_dest = x_d, y_dest = y_d;
  42.       ride_time_in_min = abs(x_s - x_d) + abs(y_s - y_d);
  43.    }
  44.  
  45.    inline bool is_catched_by (const booking &o) { // o is the ride which came earlier
  46.       int get_to_location = abs(x_src - o.x_dest) + abs(y_src - o.y_dest);
  47.       int time_to_reach = time_min + (time_min < o.time_min ? 24 * 60 : 0);
  48.       int time_reached = o.time_min + get_to_location  + 1 + o.ride_time_in_min;
  49.       return (time_reached <= time_to_reach);
  50.    }
  51. };
  52.  
  53. vector <booking> schedule;
  54.  
  55. bool vis[N];
  56. int match[N];
  57. vector < vector <int> > Adj;
  58. int n, nl, nr; // first 0, ..., nl - 1 vertices in left nl, ..., nr - 1 in right
  59.  
  60. void init() {
  61.    n = nl + nr;
  62.    assert(n);
  63.    Adj.assign(n, vector <int> ());
  64.    for (int i = 0; i < n; ++i)
  65.       vis[i] = false, match[i] = -1;
  66. }
  67.  
  68. inline void add_edge(int u, int v) {
  69.    Adj[u].push_back(v);
  70. }
  71.  
  72. bool augment(int u) {
  73.    if (vis[u]) return false;
  74.    vis[u] = true;
  75.  
  76.    for (int v : Adj[u]) {
  77.       if (match[v] == -1 || augment(match[v])) {
  78.          match[v] = u;
  79.          return true;
  80.       }
  81.    }
  82.    return false;
  83. }
  84.  
  85. int MCBM() {
  86.    assert (nl + nr == n);
  87.    int mcbm = 0;
  88.  
  89.    for (int u = 0; u < nl; ++u) { // in left set
  90.       fill(vis, vis + n, false);
  91.       mcbm += augment(u);
  92.    }
  93.    return mcbm;
  94. }
  95. int books;
  96. void solve() {
  97.    int mcbm = MCBM();
  98.    cout << books - mcbm << '\n';
  99. }
  100.  
  101. void read() {
  102.    schedule.clear();
  103.    cin >> books;
  104.    nr = nl = books;
  105.    init();
  106.  
  107.    for (int i = 0; i < books; ++i) {
  108.       int hr, mn, x_s, y_s, x_d, y_d;
  109.       char ch;
  110.       cin >> hr >> ch >> mn >> x_s >> y_s >> x_d >> y_d;
  111.  
  112.       schedule.push_back(booking(hr, mn, x_s, y_s, x_d, y_d));
  113.  
  114.       for (int j = 0; j < i; ++j) {
  115.          if (schedule[i].is_catched_by(schedule[j]))
  116.             add_edge(j, i);
  117.       }
  118.    }
  119. }
  120.  
  121. signed main() {
  122.    IOS; PREC;
  123.    int tc;
  124.    cin >> tc;
  125.    while (tc--) {
  126.       read();
  127.       solve();
  128.    }
  129.  
  130.    return EXIT_SUCCESS;
  131. }
Add Comment
Please, Sign In to add comment