zoukankan      html  css  js  c++  java
  • B

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Appoint description:
     

    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搜索

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <stack>
    11 #include <queue>
    12 #include <sstream>
    13 #include <iomanip>
    14 using namespace std;
    15 const int INF=0x5fffffff;
    16 const int EXP=1e-5;
    17 const int MS=205;
    18 struct node
    19 {
    20     int v[3],dist;   //这里的dist表示到达这个状态所倒的水量
    21     bool operator < (const node &a)const
    22     {
    23         return dist>a.dist;
    24     }
    25 };
    26 
    27 int vis[MS][MS],cap[3],ans[MS];
    28 
    29 void updata(const node &u)
    30 {
    31     for(int i=0;i<3;i++)
    32     {
    33         int d=u.v[i]; // 对每个体积进行更新
    34         if(ans[d]<0||u.dist<ans[d])
    35             ans[d]=u.dist;
    36     }
    37 }
    38 
    39 void solve(int a,int b,int c,int d)
    40 {
    41     cap[0]=a;cap[1]=b;cap[2]=c;
    42     memset(vis,0,sizeof(vis));
    43     memset(ans,-1,sizeof(ans));
    44     priority_queue<node> que;
    45     node start;
    46     start.dist=0;
    47     start.v[0]=0;
    48     start.v[1]=0;
    49     start.v[2]=c;
    50     que.push(start);
    51     while(!que.empty())
    52     {
    53         node u=que.top();
    54         que.pop();
    55         updata(u);
    56         if(ans[d]>=0)
    57             break;
    58         for(int i=0;i<3;i++)
    59             for(int j=0;j<3;j++)     //i-->j;
    60                 if(i!=j)
    61         {
    62             if(u.v[i]==0||u.v[j]==cap[j])
    63                 continue;   //i没有水  or  j已经满了
    64             int cnt=min(cap[j],u.v[i]+u.v[j])-u.v[j];
    65             node t;
    66             memcpy(&t,&u,sizeof(u));   //  t=u;
    67             t.dist=u.dist+cnt;
    68             t.v[i]-=cnt;
    69             t.v[j]+=cnt;
    70             if(!vis[t.v[0]][t.v[1]])
    71             {
    72                 vis[t.v[0]][t.v[1]]=1;
    73                 que.push(t);
    74             }
    75         }
    76     }
    77     while(d>=0)
    78     {
    79         if(ans[d]>=0)
    80         {
    81             printf("%d %d
    ",ans[d],d);
    82             return ;
    83         }
    84         d--;
    85     }
    86 }
    87 
    88 int main()
    89 {
    90     int T,a,b,c,d;
    91     scanf("%d",&T);
    92     while(T--)
    93     {
    94         scanf("%d%d%d%d",&a,&b,&c,&d);
    95         solve(a,b,c,d);
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    禁止360开机自动启动
    Google Code注册方法详解 Google Code网盘申请方法
    做程序开发工作,编程思想很重要
    EPP(Eclipse PHP)语法高亮仿EditPlus配置
    2HC32F460(华大)+BC260Y(NBIOT)基本控制篇(自建物联网平台)整体运行测试微信小程序扫码绑定BC260Y(NBIOT),并通过MQTT和单片机实现远程通信控制
    2HC32F460(华大)+BC260Y(NBIOT)基本控制篇(自建物联网平台)整体运行测试Android扫码绑定BC260Y(NBIOT),并通过MQTT和单片机实现远程通信控制
    【面向对象】宽接口、窄接口和访问方法(上)
    重构,小步进行曲
    Java中有些好的特性(一):静态导入
    【读书笔记】设计模式沉思录
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4330524.html
Copyright © 2011-2022 走看看