YEZAELP

SMMR-226: Two Water Tanks

Jun 8th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF=1e9;
  4. using pi = pair<int,int>;
  5. using pii = pair< int , pair<int,int> >;
  6. int main(){
  7.     int t;
  8.     scanf("%d",&t);
  9.     while(t--){
  10.         int n,m,want;
  11.         scanf("%d %d %d",&n,&m,&want);
  12.  
  13.         map< pi , bool > visited;
  14.  
  15.         priority_queue <pii,vector<pii>,greater<pii>> pq;
  16.  
  17.         pq.push({ 0 , {0,0} });
  18.         bool fl=false;
  19.  
  20.         while(pq.size()>0){
  21.             int v1,v2,d;
  22.             d=pq.top().first;
  23.             v1=pq.top().second.first;
  24.             v2=pq.top().second.second;
  25.             pq.pop();
  26.  
  27.             if(visited[{v1,v2}]) continue;
  28.             visited[{v1,v2}] = true;
  29.  
  30.             if(v1==want||v2==want){
  31.                 printf("%d",d);
  32.                 fl=true;
  33.                 break;
  34.             }
  35.  
  36.             if(v1==0)//fill_1
  37.                 pq.push({d+1,{n,v2} });
  38.  
  39.             if(v2==0)//fill_2
  40.                 pq.push({d+1,{v1,m} });
  41.  
  42.             if(v1!=0)//throw away_1
  43.                 pq.push({d+1,{0,v2} });
  44.  
  45.             if(v2!=0)//throw away_2
  46.                 pq.push({d+1,{v1,0} });
  47.  
  48.             if(v1!=n){
  49.                 v1=v1+v2;
  50.                 if(v1>n){
  51.                     v2=v1-n;
  52.                     v1=n;
  53.                 }
  54.                 else v2=0;
  55.                 pq.push({d+1,{v1,v2}});
  56.             }
  57.             if(v2!=m){
  58.                 v2=v1+v2;
  59.                 if(v2>m){
  60.                     v1=v2-m;
  61.                     v2=m;
  62.                 }
  63.                 else v1=0;
  64.                 pq.push({d+1,{v1,v2}});
  65.             }
  66.  
  67.         }
  68.         if(!fl) printf("-1");
  69.         printf("\n");
  70.     }
  71.  
  72.     return 0;
  73. }
Add Comment
Please, Sign In to add comment