Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fst first
  5. #define snd second
  6. #define mp make_pair
  7. #define rep(i,a,n) for(int i=(a);i<(n);++i)
  8. #define per(i,a,n) for(int i=(n)-1;i>=(a);--i)
  9. #define all(x) (x).begin(),(x).end()
  10.  
  11. template <typename T1, typename T2>
  12. bool umin(T1& x, const T2& y){return x>y ? x=y,true : false;}
  13.  
  14. template <typename T1, typename T2>
  15. bool umax(T1& x, const T2& y){return x<y ? x=y,true : false;}
  16.  
  17. typedef long long ll;
  18. typedef long double ld;
  19. typedef pair<int,int> pii;
  20.  
  21. const int N = (int)1e6+3;
  22. const int mod = (int)1e9+7;
  23. const int INF = (int)1e9+17;
  24. const ll LLINF = (ll)1e18+17;
  25.  
  26.  
  27. int n, m;
  28. int res;
  29. int a[N];
  30. int d[N+N];
  31. bool mark[N+N];
  32.  
  33. void solve() {
  34.     priority_queue<pii, vector<pii>, greater<pii> > q;
  35.     fill(d, d+N+N, INF);
  36.     d[a[0]] = 0;
  37.     q.push(mp(0, a[0]));
  38.     while (!q.empty()) {
  39.         int v = q.top().snd;
  40.         int dist = q.top().fst;
  41. //        cerr << v << ' ' << dist << endl;
  42.         q.pop();
  43.         if (dist != d[v]) continue;
  44. //        cerr << "here" << endl;
  45.         if (mark[v]) {
  46.             res += dist;
  47.             d[v] = dist = 0;
  48.         }
  49.         rep(i, 0, n) {
  50.             int to = v ^ (1<<i);
  51. //            cerr << to << ' ' << d[to] << endl;
  52.             if (umin(d[to], d[v]+1)) {
  53.                 q.push(mp(d[to], to));
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. int main() {
  60.     ios_base::sync_with_stdio(false);
  61.     cin.tie(nullptr), cout.tie(nullptr);
  62. #ifdef LOCAL
  63.     freopen("input.txt", "r", stdin);
  64.     freopen("output.txt", "w", stdout);
  65. #endif
  66.  
  67.     //!slow
  68.     cin >> n >> m;
  69.     rep(i, 0, m) {
  70.         string s;
  71.         cin >> s;
  72.         rep(j, 0, n) a[i] |= ((s[j]=='R')<<j);
  73. //        cerr << a[i] << '\n';
  74.         mark[a[i]] = true;
  75.     }
  76.     solve();
  77.     cout << res << endl;
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement