【例题 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值