zoukankan      html  css  js  c++  java
  • uva 10603 Fill

    题目大意就是倒水问题。 指定了一个最终状态d 。 如果达不到d ,则求出最接近d 的一个解

    熟悉了一下隐式图搜索

    题目:

    Problem D

    Fill

    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

    代码:

     1 #include <iostream>
     2 #include <queue>
     3 #include <algorithm>
     4 #include <memory.h>
     5 using namespace std;
     6 
     7 const int maxn = 1000;
     8 struct node
     9 {
    10     int vol[3];
    11     int total;
    12     node(){memset(vol,0,sizeof(vol));total=0;}
    13 };
    14 
    15 int vis[maxn][maxn];
    16 int cap[3],d;
    17 int qf=0,qr=1;
    18 node v[maxn*maxn];
    19 int bfs()
    20 {
    21     qf=0;qr=1;
    22     v[qf].vol[2]=cap[2];
    23     vis[v[qf].vol[0]][v[qf].vol[1]]=1;
    24     while(qf<qr)
    25     {
    26         node& now=v[qf];
    27         if(now.vol[0]==d || now.vol[1]==d || now.vol[2] == d)
    28         {
    29             cout<<now.total<<" "<<d<<endl;
    30             return 1;
    31         }
    32         node  an ;
    33 
    34         for(int i=0;i<3;i++)
    35         {
    36             for(int j=0;j<3;j++)
    37             {
    38                 if(i!=j)
    39                 {
    40                      for(int i=0;i<3;i++)an.vol[i] = now.vol[i];
    41                     int amount = min(now.vol[i],cap[j]-now.vol[j]);
    42                     an.vol[i]= now.vol[i]-amount;
    43                     an.vol[j] = now.vol[j]+ amount;
    44                     an.total = now.total+ amount;
    45                     if(!vis[an.vol[0]][an.vol[1]])
    46                     {
    47                    //    cout<<"amount :"<<amount<<"  "<<" I : "<<i<<" J :"<<j<<"  "<<an.vol[0]<<" "<<an.vol[1]<<" "<<an.vol[2]<<endl;
    48                         v[qr++] = an ;
    49                         vis[an.vol[0]][an.vol[1]]=1;
    50                     }
    51                 }
    52             }
    53         }
    54         qf++;
    55     }
    56 
    57     return 0;
    58 }
    59 
    60 
    61 int main()
    62 {
    63     int tst;
    64     cin>>tst;
    65     while(tst--)
    66     {
    67         cin>>cap[0]>>cap[1]>>cap[2]>>d;
    68 
    69         int flag = 0;
    70         flag  = bfs();
    71         if(!flag )
    72         {
    73             for(int m = d-1;m>=0;m--)
    74             for(int i=0;i<qr;i++)
    75             {
    76                 if(v[i].vol[0]==m||v[i].vol[1]==m||v[i].vol[2]==m)
    77                 {
    78                     cout<<v[i].total<<" "<<m<<endl;
    79                     flag = true;
    80                     break;
    81                 }
    82             }
    83             if(flag )break;
    84         }
    85         memset(vis,0,sizeof(vis));
    86     }
    87 
    88 
    89     return 0;
    90 }
  • 相关阅读:
    网站常见的报错
    数据库的备份与还原
    纵向tab标签切换效果
    CSS3动画 transition 过渡,宽度由中间向两边发生过渡
    移入切换图片路径;移出再切回去
    动态修改手机号
    json基本介绍及使用
    py 封装 操作json 的方法
    通过htmltestrunner模块美化报告
    记:运行测试case遇到的问题!
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3485910.html
Copyright © 2011-2022 走看看