Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int state, weight;
- bool operator < (const A&o) const{
- return weight > o.weight;
- }
- };
- priority_queue<A > heap;
- vector<A > g[80010];
- int dis[80010];
- int poisonState[80010];
- int main(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n,s,w;
- cin >> n >> s;
- for(int i=1;i<=n;i++){
- cin >> w;
- int poison = 0, antidote = 0;
- for(int j=0;j<s;j++){
- int num;
- cin >> num;
- if(num == -1){
- // poison
- poison|=(1<<j);
- }else if(num == 1){
- // antidote
- antidote|=(1<<j);
- }
- }
- g[poison].push_back({antidote,w});
- poisonState[i] = poison;
- // poisonState เก็บว่า ถ้ากินผลไม้ชิ้นที่ i ไปจะทำให้เป็น poison state ไหน
- }
- for(int i=0;i<(1<<s);i++){
- for(int j=0;j<s;j++){
- if((i&(1<<j)) == 0)
- g[i|(1<<j)].push_back({i,0});
- }
- dis[i] = 1e9;
- }
- dis[0] = 0;
- heap.push({0,0});
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- for(auto x:g[now.state]){
- if(dis[x.state] <= now.weight + x.weight) continue;
- dis[x.state] = now.weight + x.weight;
- heap.push({x.state,dis[x.state]});
- }
- }
- int mx = 0;
- for(int i=1;i<=n;i++){
- if(dis[poisonState[i]] != 1e9)
- mx = max(mx,dis[poisonState[i]]);
- }
- cout << mx << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment