Advertisement
vlatkovski

uva 321 WA

Oct 26th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <utility>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <functional>
  8. #include <iterator>
  9. #include <bitset>
  10. #include <vector>
  11. #include <set>
  12. #include <map>
  13. #include <unordered_set>
  14. #include <unordered_map>
  15. #include <string>
  16. #include <sstream>
  17. #include <queue>
  18. #include <deque>
  19. #include <list>
  20. #include <stack>
  21. using namespace std;
  22. // printing
  23. #ifdef _DEBUG
  24. #define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); _dbg(_it, args); }
  25. void _dbg(istream_iterator<string> it){++it;}
  26. template<typename T, typename... Args>
  27. void _dbg(istream_iterator<string> it, T a, Args... args){
  28.   if (*it=="'\\n'" || *it=="\"\\n\"" || *it=="endl") { cout << "\n"; }
  29.   else { cout << setw(8) << std::left << *it << " = " << setw(4) << std::right << a << "\n"; }
  30.   _dbg(++it, args...); }
  31. template<typename T>
  32. std::ostream& _containerprint(std::ostream &out, T const &val) { return (out << setw(4) << val << " "); }
  33. template<typename T1, typename T2>
  34. std::ostream& _containerprint(std::ostream &out, std::pair<T1, T2> const &val) { return (out << "{" << setw(4) << val.first << " " << setw(4) << val.second << "} "); }
  35. template<template<typename, typename...> class TT, typename... Args>
  36. std::ostream& operator<<(std::ostream &out, const TT<Args...> &cont) { for(auto&& elem : cont) { _containerprint(out, elem); } return out; }
  37. std::ostream& operator<<(std::ostream &out, const std::string &s) { return operator << <char>(out, s); }
  38. #endif
  39. // other
  40. #define endl '\n'
  41. #define all(v) begin(v), end(v)
  42. typedef long long ll;
  43. typedef long double ld;
  44. typedef std::pair<int, int> ii;
  45. typedef std::vector<ii> vii;
  46. typedef std::vector<int> vi;
  47. const double EPS = 1e-9;
  48. const int mod = 998244353;
  49. template<typename T> T gcd(T a, T b){T c; while(b){c=b; b=a%b; a=c;} return a;}
  50. template<typename T> T powmod(T a, T b){T res = 1; a %= mod; while(b){if(b&1)res=res*(a%mod); a=a*(a%mod); b>>=1;} return res;}
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. int V, E, S;
  58. vector<int> G[12];
  59. vector<int> light[12];
  60.  
  61. int dist[12][1025];
  62. ii pre[12][1025];
  63.  
  64. void bfs(ii ts) {
  65.   queue<ii> q;
  66.   dist[ts.first][ts.second] = 0;
  67.   q.push(ts);
  68.   while (!q.empty()) {
  69.     ts = q.front(); q.pop();
  70.     int u = ts.first, bm = ts.second;
  71.     for (int l : light[u]) {
  72.       int bm1 = bm ^ (1 << l);
  73.       if ((bm1 & (1 << u)) && dist[u][bm1] == -1) {
  74.         dist[u][bm1] = dist[u][bm] + 1;
  75.         pre[u][bm1] = ii(u, bm);
  76.         q.emplace(u, bm1);
  77.       }
  78.     }
  79.     for (int v : G[u]) {
  80.       if ((bm & (1 << v)) && dist[v][bm] == -1) {
  81.         dist[v][bm] = dist[u][bm] + 1;
  82.         pre[v][bm] = ii(u, bm);
  83.         q.emplace(v, bm);
  84.       }
  85.     }
  86.   }
  87. }
  88.  
  89. void Solve() {
  90.   int TC = 1;
  91.   while (cin >> V >> E >> S, V != 0 || E != 0 || S != 0) {
  92.     if (TC > 1) cout << endl;
  93.     for (int u = 0; u < 12; ++u) G[u].clear(), light[u].clear();
  94.     memset(dist, -1, sizeof(dist));
  95.     for (int i = 0; i < E; ++i) {
  96.       int u, v;
  97.       cin >> u >> v;
  98.       --u, --v;
  99.       G[u].push_back(v);
  100.       G[v].push_back(u);
  101.     }
  102.     for (int i = 0; i < S; ++i) {
  103.       int u, v;
  104.       cin >> u >> v;
  105.       --u, --v;
  106.       light[u].push_back(v);
  107.     }
  108.  
  109.     pre[0][1] = {-1, -1};
  110.     bfs(ii(0, 1));
  111.  
  112.     cout << "Villa #" << TC++ << "\n";
  113.     int u = V-1, bm = 1 << (V-1);
  114.     if (dist[u][bm] == -1) {
  115.       cout << "The problem cannot be solved.\n";
  116.     } else {
  117.       cout << "The problem can be solved in " << dist[u][bm] << " steps:\n";
  118.       vector<string> ans;
  119.       while (u != -1) {
  120.         int pu = pre[u][bm].first, pbm = pre[u][bm].second;
  121.         if (pu == u) {
  122.           int diff = __builtin_ffs(bm ^ pbm); //position of lsb (1-indexed)
  123.           if (bm & (1 << diff)) ans.push_back("- Switch off light in room " + to_string(diff) + ".");
  124.           else ans.push_back("- Switch on light in room " + to_string(diff) + ".");
  125.         } else {
  126.           ans.push_back("- Move to room " + to_string(u+1) + ".");
  127.         }
  128.         u = pu, bm = pbm;
  129.       }
  130.       for (int i = ans.size()-2; i >= 0; --i) {
  131.         cout << ans[i] << endl;
  132.       }
  133.     }
  134.   }
  135. }
  136.  
  137.  
  138. int main() {
  139.   std::ios::sync_with_stdio(false);
  140.   std::cin.tie(nullptr);
  141.   #ifdef _DEBUG
  142. //  std::freopen("in.txt", "r", stdin);
  143. //  std::freopen("out.txt", "w", stdout);
  144.   #endif
  145.   Solve();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement