Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third
is completely filled with water. It is allowed to pour water from one jug into another until either the first one is empty or the second one is full. This operation can be performed zero, one or more times.
You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should find a smaller amount of water d' < d which is closest to d and for which d' liters could be produced. When d' is found, your program should compute the least total amount of poured water needed to produce d' liters in at least one of the jugs.
Input
The first line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input containing four space separated integers - a, b, c and d.
Output
The output consists of two integers separated by a single space. The first integer equals the least total amount (the sum of all waters you pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations, or equals the closest smaller value d' that your program has found.
Sample Input |
Sample Output |
2 2 3 4 2 96 97 199 62 |
2 2 9859 62 |
Problem source: Bulgarian National Olympiad in Informatics 2003
Problem submitter: Ivaylo Riskov
Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury
倒水问题 状态bfs搜索
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x5fffffff;
const int EXP=1e-;
const int MS=;
struct node
{
int v[],dist; //这里的dist表示到达这个状态所倒的水量
bool operator < (const node &a)const
{
return dist>a.dist;
}
}; int vis[MS][MS],cap[],ans[MS]; void updata(const node &u)
{
for(int i=;i<;i++)
{
int d=u.v[i]; // 对每个体积进行更新
if(ans[d]<||u.dist<ans[d])
ans[d]=u.dist;
}
} void solve(int a,int b,int c,int d)
{
cap[]=a;cap[]=b;cap[]=c;
memset(vis,,sizeof(vis));
memset(ans,-,sizeof(ans));
priority_queue<node> que;
node start;
start.dist=;
start.v[]=;
start.v[]=;
start.v[]=c;
que.push(start);
while(!que.empty())
{
node u=que.top();
que.pop();
updata(u);
if(ans[d]>=)
break;
for(int i=;i<;i++)
for(int j=;j<;j++) //i-->j;
if(i!=j)
{
if(u.v[i]==||u.v[j]==cap[j])
continue; //i没有水 or j已经满了
int cnt=min(cap[j],u.v[i]+u.v[j])-u.v[j];
node t;
memcpy(&t,&u,sizeof(u)); // t=u;
t.dist=u.dist+cnt;
t.v[i]-=cnt;
t.v[j]+=cnt;
if(!vis[t.v[]][t.v[]])
{
vis[t.v[]][t.v[]]=;
que.push(t);
}
}
}
while(d>=)
{
if(ans[d]>=)
{
printf("%d %d\n",ans[d],d);
return ;
}
d--;
}
} int main()
{
int T,a,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
solve(a,b,c,d);
}
return ;
}