Advertisement
LZsolar

SMMR-226: Two Water Tanks

Mar 31st, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int lld;
  4. typedef pair<lld,pair<lld,lld>> pipii;
  5.  
  6. int water(lld n,lld m,lld d){
  7.    
  8.     map<int,map<int,bool>> visited;
  9.     priority_queue<pipii,vector<pipii>,greater<pipii>> q;
  10.     q.push({0,{0,0}});
  11.    
  12.     while(!q.empty()){
  13.         lld u=q.top().second.first,v=q.top().second.second,k=q.top().first;
  14.         q.pop();
  15.        
  16.         if(u==d||v==d){return k;}
  17.        
  18.         if(visited[u][v]){continue;}
  19.         visited[u][v]=true;
  20.        
  21.         //เติมน้ำ
  22.         if(u<n){q.push({k+1,{n,v}});}
  23.         if(v<m){q.push({k+1,{u,m}});}
  24.         //เทน้ำออก
  25.         if(u>0){q.push({k+1,{0,v}});}
  26.         if(v>0){q.push({k+1,{u,0}});}
  27.         //ย้ายน้ำ  u->v
  28.         if(u>0&&v<m){
  29.             lld a= u-(m-v); if(a<0){a=0;}
  30.             lld b= u+v; if(b>m){b=m;}
  31.             q.push({k+1,{a,b}});
  32.         }
  33.         //ย้ายน้ำ  v->u
  34.         if(u<n&&v>0){
  35.             lld a=u+v; if(a>n){a=n;}
  36.             lld b=v-(n-u); if(b<0){b=0;}
  37.             q.push({k+1,{a,b}});
  38.         }
  39.     }
  40.     return -1;
  41. }
  42.  
  43.  
  44.  
  45. int main(){
  46.     int t; scanf("%d",&t);
  47.     for(int p=0;p<t;p++){
  48.     lld n,m,d;
  49.     scanf("%lld %lld %lld",&n,&m,&d);
  50.     lld ans=water(n,m,d);
  51.     printf("%lld\n",ans);
  52.    
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement