Dang_Quan_10_Tin

A

Jul 20th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <unordered_set>
  6. #include <queue>
  7. #define task ""
  8. using namespace std;
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. const int N = 2e3 + 2;
  13. struct way{
  14.     int v, w;
  15.     way() {}
  16.     way(int x, int y){
  17.         v = x;
  18.         w = y;
  19.     }
  20. };
  21. vector<way> adj[N];
  22. int goods[N];
  23. int n, m, k;
  24. bool ck[12];
  25. int ans(1e9);
  26. int res[N];
  27.  
  28. void Read(){
  29.     cin >> n >> m >> k;
  30.     for(int i = 1; i <= n; ++i){
  31.         int k;
  32.         cin >> k;
  33.         while(k--){
  34.             int v;
  35.             cin >> v;
  36.             goods[i] |= 1 << v;
  37.         }
  38.     }
  39.     for(int i = 1; i <= m; ++i){
  40.         int u, v, w;
  41.         cin >> u >> v >> w;
  42.         adj[u].push_back(way(v, w));
  43.         adj[v].push_back(way(u, w));
  44.     }
  45. }
  46.  
  47. int Dijkstra(int &a){
  48.     vector<vector<bool>> ck(N, vector<bool>(N));
  49.     vector<int> ans(N);
  50.     fill(ans.begin(), ans.end(), 12);
  51.     struct Tque{
  52.         int v, w, a;
  53.         Tque () {}
  54.         Tque(int m, int n, int p){
  55.             v = m; w = n; a = p;
  56.         }
  57.         bool operator < (const Tque &x) const {
  58.             return w > x.w ||
  59.                 (w == x.w && __builtin_popcount(a) > __builtin_popcount(x.a));
  60.         }
  61.     };
  62.     a &= ~goods[1];
  63.     priority_queue<Tque> s;
  64.     s.push(Tque(1, 0, a));
  65.     while(s.size()){
  66.         Tque c = s.top();
  67.         s.pop();
  68.         if(c.v == n && c.a == 0)
  69.             return c.w;
  70.         if(ck[c.v][c.a] || __builtin_popcount(c.a) > ans[c.v])
  71.             continue;
  72.         ck[c.v][c.a] = 1;
  73.         ans[c.v] = __builtin_popcount(c.a);
  74.         for(auto i : adj[c.v]){
  75.             int aa = c.a;
  76.             aa &= ~goods[i.v];
  77.             if(!ck[i.v][aa] && __builtin_popcount(aa) <= ans[i.v])
  78.                 s.push(Tque(i.v, c.w + i.w, aa));
  79.         }
  80.     }
  81. }
  82.  
  83. void Solve(){
  84.     int I = 0, S = 0;
  85.     for(int i = 1; i <= k; ++i)
  86.         if(ck[i])
  87.             I |= 1 << i;
  88.     res[I] = Dijkstra(I);
  89. }
  90.  
  91. void Update_min(){
  92.     int I = 0, S = 0;
  93.     for(int i = 1; i <= k; ++i)
  94.         if(ck[i])
  95.             I |= 1 << i;
  96.         else
  97.             S |= 1 << i;
  98.         I &= ~goods[1];
  99.         S &= ~goods[1];
  100.     ans = min(ans, max(res[I], res[S]));
  101. }
  102.  
  103. void dfs(int i, bool check){
  104.     if(i > k){
  105.         if(check)
  106.             Solve();
  107.         else
  108.             Update_min();
  109.         return;
  110.     }
  111.     if(goods[1] & (1 << i))
  112.         goto done;
  113.     ck[i] = 1;
  114.     dfs(i + 1, check);
  115.     ck[i] = 0;
  116.     done:;
  117.     dfs(i + 1, check);
  118. }
  119.  
  120. main(){
  121.     ios::sync_with_stdio(0);
  122.     cin.tie(0);
  123.     cout.tie(0); if(fopen(task".INP", "r"))
  124.     freopen(task".INP", "r", stdin),
  125.     freopen(task".OUT", "w", stdout);
  126.     Read();
  127.     dfs(1, 1);
  128.     dfs(1, 0);
  129.     cout << ans;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment