MAGCARI

Phitsanulok

Jun 19th, 2023
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct A{
  4.     int state, weight;
  5.     bool operator < (const A&o) const{
  6.         return weight > o.weight;
  7.     }
  8. };
  9. priority_queue<A > heap;
  10. vector<A > g[80010];
  11. int dis[80010];
  12. int poisonState[80010];
  13. int main(){
  14.     cin.tie(0)->sync_with_stdio(0);
  15.     cin.exceptions(cin.failbit);
  16.     int n,s,w;
  17.     cin >> n >> s;
  18.     for(int i=1;i<=n;i++){
  19.         cin >> w;
  20.         int poison = 0, antidote = 0;
  21.         for(int j=0;j<s;j++){
  22.             int num;
  23.             cin >> num;
  24.             if(num == -1){
  25.                 // poison
  26.                 poison|=(1<<j);
  27.             }else if(num == 1){
  28.                 // antidote
  29.                 antidote|=(1<<j);
  30.             }
  31.         }
  32.         g[poison].push_back({antidote,w});
  33.         poisonState[i] = poison;
  34.         // poisonState เก็บว่า ถ้ากินผลไม้ชิ้นที่ i ไปจะทำให้เป็น poison state ไหน
  35.     }
  36.     for(int i=0;i<(1<<s);i++){
  37.         for(int j=0;j<s;j++){
  38.             if((i&(1<<j)) == 0)
  39.                 g[i|(1<<j)].push_back({i,0});
  40.         }
  41.         dis[i] = 1e9;
  42.     }
  43.     dis[0] = 0;
  44.     heap.push({0,0});
  45.     while(!heap.empty()){
  46.         A now = heap.top();
  47.         heap.pop();
  48.         for(auto x:g[now.state]){
  49.             if(dis[x.state] <= now.weight + x.weight)   continue;
  50.             dis[x.state] = now.weight + x.weight;
  51.             heap.push({x.state,dis[x.state]});
  52.         }
  53.     }
  54.     int mx = 0;
  55.     for(int i=1;i<=n;i++){
  56.         if(dis[poisonState[i]] != 1e9)
  57.             mx = max(mx,dis[poisonState[i]]);
  58.     }
  59.     cout << mx << '\n';
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment