【例题 7-8 UVA - 10603】Fill
【链接】 我是链接,点我呀:)
【题意】
【题解】
三维显然可以缩短为2维。 只要知道a,b瓶中的水量,c瓶中的水量减一下就能得到。 则设dis[a][b]表示a,b瓶中水量为a,b时,水量的移动量。 然后做一下二维的spfa. 最后枚举a,b得到对应答案就好。【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 200;
int bo[N+10][N+10];
bool inq[N+10][N+10];
int T,d,tot;
queue <pair<int,int> > dl;
int maxv[3];
int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin >> T;
while (T--){
memset(inq,0,sizeof inq);
memset(bo,-1,sizeof bo);
for (int i = 0;i < 3;i++) cin >> maxv[i];cin >> d;
tot = maxv[2];
bo[0][0] = 0;
inq[0][0] = true;
dl.push(make_pair(0,0));
vector <int> v;v.resize(3);
while (!dl.empty()){
pair<int,int> temp = dl.front();dl.pop();
v[0] = temp.first,v[1] = temp.second,v[2] = tot-v[0]-v[1];
inq[v[0]][v[1]] = false;
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
if (i!=j){
//i->j
if (v[i]==0 || v[j]==maxv[j]) continue;
int ta = v[0],tb = v[1];
int temp = min(maxv[j]-v[j],v[i]);
v[i]-=temp,v[j]+=temp;
int a = v[0],b = v[1];
if (bo[a][b]==-1 || bo[a][b] > bo[ta][tb] + temp){
bo[a][b] = bo[ta][tb] + temp;
if (!inq[a][b]){
inq[a][b] = true;
dl.push(make_pair(a,b));
}
}
v[i]+=temp,v[j]-=temp;
}
}
int now = 3000,usage = -1;
for (int i = 0;i <= maxv[0];i++)
for(int j = 0;j <= maxv[1];j++)
if (bo[i][j]!=-1){
v[0] = i,v[1] = j,v[2] = tot-i-j;
for (int k = 0;k < 3;k++){
int x = v[k];
if (x <= d){
if (now==3000 || x > now){
now = x;
usage = bo[i][j];
}else if (x==now){
usage = min(usage,bo[i][j]);
}
}
}
}
cout << usage <<' '<<now<<endl;
}
return 0;
}