TrickmanOff

Всесиб

Oct 29th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <vector>
  5. #include <queue>
  6. #include <functional>
  7. #include <set>
  8. #include <map>
  9. #include <math.h>
  10. #include <cmath>
  11. #include <string>
  12. #include <time.h>
  13. #include <random>
  14. #include <unordered_set>
  15. #include <unordered_map>
  16. #include <bitset>
  17. #include <string.h>
  18. #include <stack>
  19. using namespace std;
  20.  
  21. #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
  22. #define cin in
  23. #define cout out
  24. #define pii pair<int,int>
  25. #define ll long long
  26. #define db double
  27. #define ld long double
  28. #define uset unordered_set
  29. #define umap unordered_map
  30. #define F first
  31. #define S second
  32. #define vec vector
  33. #define ms multiset
  34. #define pb push_back
  35. #define pll pair<ll,ll>
  36. #define pdd pair<ld, ld>
  37. #define pq priority_queue
  38. #define umap unordered_map
  39. #define uset unordered_set
  40. #define pii pair<int, int>
  41. #define pll pair<ll, ll>
  42. #define pnn pair<Node*, Node*>
  43. #define uid uniform_int_distribution
  44.  
  45. ifstream in("input.txt");
  46. ofstream out("output.txt");
  47.  
  48. const int INF = 2e9;
  49. int n, m, q, xs, ys;
  50.  
  51. vector<vector<int>> field;
  52. vector<int> vals;
  53. vector<pii> pts;
  54.  
  55. void input() {
  56.     cin >> n >> m >> q;
  57.     field.resize(n, vector<int>(m));
  58.    
  59.     cin >> xs >> ys;
  60.     xs--; ys--;
  61.  
  62.     for (int i = 0; i < n; i++) {
  63.         string s;
  64.         cin >> s;
  65.         for (int j = 0; j < m; j++) {
  66.             if (s[j] == '.')
  67.                 field[i][j] = 0;
  68.             else
  69.                 field[i][j] = INF;
  70.         }
  71.     }
  72.  
  73.     for (int i = 0; i < q; i++) {
  74.         int x, y, c;
  75.         cin >> x >> y >> c;
  76.  
  77.         x--; y--;
  78.         vals.push_back(c);
  79.         pts.push_back({ x, y });
  80.  
  81.         field[x][y] = i+1;
  82.     }
  83. }
  84.  
  85. struct area {
  86.     int x, y;
  87.     ll val;
  88. };
  89.  
  90. vector<pii> turns = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} };
  91.  
  92. bool validxy(int x, int y) {
  93.     if (x >= 0 && x < n && y >= 0 && y < m)
  94.         return 1;
  95.     else
  96.         return 0;
  97. }
  98.  
  99. vector<vector<int>> g;
  100. vector<bool> from_start;
  101.  
  102. void bfs(int num) {
  103.     vector<vector<bool>> used(n, vector<bool>(m, 0));
  104.  
  105.     queue<pii> q;
  106.     q.push(pts[num]);
  107.     used[pts[num].first][pts[num].second] = 1;
  108.    
  109.     while (!q.empty()) {
  110.         pii cur = q.front();
  111.         q.pop();
  112.  
  113.         int x = cur.first, y = cur.second;
  114.  
  115.         if (x == xs && y == ys)
  116.             from_start[num] = 1;
  117.  
  118.         if (field[x][y] != 0 && cur != pts[num]) {
  119.             g[num][field[x][y] - 1] = 1;
  120.             g[field[x][y] - 1][num] = 1;
  121.             continue;
  122.         }
  123.        
  124.         for (pii turn : turns) {
  125.             int cur_x = x + turn.first;
  126.             int cur_y = y + turn.second;
  127.  
  128.             if (validxy(cur_x, cur_y) && !used[cur_x][cur_y] && field[cur_x][cur_y] != INF) {
  129.                 q.push({ cur_x, cur_y });
  130.                 used[cur_x][cur_y] = 1;
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136. void transform() {
  137.     from_start.resize(q, 0);
  138.     g.resize(q, vector<int>(q, 0));
  139.  
  140.     for (int i = 0; i < q; i++)
  141.         bfs(i);
  142. }
  143.  
  144. bool bit(int a, int pos) {
  145.     return ((a >> pos) % 2);
  146. }
  147.  
  148. ll cnt(int mask) {
  149.     ll cur = 0;
  150.     for (int i = 0; i < q; i++) {
  151.         if (bit(mask, i))
  152.             cur += vals[i];
  153.     }
  154.     return cur;
  155. }
  156.  
  157. void dfs(int v, vector<bool>& used, vector<bool> &mask) {
  158.     used[v] = 1;
  159.  
  160.     for (int to = 0; to < q; to++) {
  161.         if (g[v][to] && !used[to] && mask[to])
  162.             dfs(to, used, mask);
  163.     }
  164. }
  165.  
  166. bool check(int mask) {
  167.     vector<bool> cur(q, 0);
  168.     for (int i = 0; i < q; i++) {
  169.         if (bit(mask, i))
  170.             cur[i] = 1;
  171.     }
  172.  
  173.     vector<bool> used(q, 0);
  174.     for (int i = 0; i < q; i++) {
  175.         if (from_start[i] && cur[i])
  176.             dfs(i, used, cur);
  177.     }
  178.  
  179.     for (int i = 0; i < q; i++) {
  180.         if (!used[i] && cur[i])
  181.             return 0;
  182.     }
  183.     return 1;
  184. }
  185.  
  186. ll ans = 0;
  187.  
  188. void gen() {
  189.     for (int i = 0; i < (1 << q); i++) {
  190.         if (check(i)) {
  191.             /*
  192.             for (int j = 0; j < q; j++) {
  193.                 if (bit(i, j))
  194.                     cout << j << ' ';
  195.             }
  196.             cout << '\n';
  197.             */
  198.  
  199.             ans = max(ans, cnt(i));
  200.         }
  201.     }
  202. }
  203.  
  204. int main() {
  205.     input();
  206.     transform();
  207.     gen();
  208.    
  209.     cout << ans;
  210. }
Add Comment
Please, Sign In to add comment