Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int lld;
- typedef pair<lld,pair<lld,lld>> pipii;
- int water(lld n,lld m,lld d){
- map<int,map<int,bool>> visited;
- priority_queue<pipii,vector<pipii>,greater<pipii>> q;
- q.push({0,{0,0}});
- while(!q.empty()){
- lld u=q.top().second.first,v=q.top().second.second,k=q.top().first;
- q.pop();
- if(u==d||v==d){return k;}
- if(visited[u][v]){continue;}
- visited[u][v]=true;
- //เติมน้ำ
- if(u<n){q.push({k+1,{n,v}});}
- if(v<m){q.push({k+1,{u,m}});}
- //เทน้ำออก
- if(u>0){q.push({k+1,{0,v}});}
- if(v>0){q.push({k+1,{u,0}});}
- //ย้ายน้ำ u->v
- if(u>0&&v<m){
- lld a= u-(m-v); if(a<0){a=0;}
- lld b= u+v; if(b>m){b=m;}
- q.push({k+1,{a,b}});
- }
- //ย้ายน้ำ v->u
- if(u<n&&v>0){
- lld a=u+v; if(a>n){a=n;}
- lld b=v-(n-u); if(b<0){b=0;}
- q.push({k+1,{a,b}});
- }
- }
- return -1;
- }
- int main(){
- int t; scanf("%d",&t);
- for(int p=0;p<t;p++){
- lld n,m,d;
- scanf("%lld %lld %lld",&n,&m,&d);
- lld ans=water(n,m,d);
- printf("%lld\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement