zoukankan      html  css  js  c++  java
  • UVA10603-Fill(BFS)

    Problem UVA10603-Fill

    Accept:1162  Submit:10693

    Time Limit: 3000 mSec

     Problem 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

    2
    2 3 4 2
    96 97 199 62
     

     Sample Ouput

    2 2

    9859 62

    题解:倒水问题,原来只写过步数最少的,而这个题问的是倒水量最少。解决方案是将普通的队列换成优先队列,每次优先取出倒水量最少的节点。

    这样做的正确性lrj表示不会证,但是可以从Dijkstra上考虑。把状态看成节点,以两个状态之间倒水量作为边权,问题就成了最短路,正确性还是有保证的。

    这个题还有一个要求是如果无解,输出小于需要得到的水量并且距离需要得到水量最近的有解的情况。一开始考虑的是维护一个距离,一个最小倒水量,但是不如lrj的做法方便:直接记录所有解,最后统计答案,代码比较好写。

     1 #include <bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 using namespace std;
     4 
     5 const int maxn = 200+10;
     6 
     7 struct Node{
     8     int v[3];
     9     int dist;
    10     Node() {}
    11     bool operator < (const Node &a)const{
    12         return dist > a.dist;
    13     }
    14 };
    15 
    16 int d,cap[3];
    17 int ans[maxn];
    18 bool vis[maxn][maxn];
    19 
    20 void update_ans(const Node &u){
    21     for(int i = 0;i < 3;i++){
    22         int d = u.v[i];
    23         if(ans[d]<0 || u.dist<ans[d]) ans[d] = u.dist;
    24     }
    25 }
    26 
    27 void bfs(){
    28     Node start;
    29     start.v[0] = 0,start.v[1] = 0,start.v[2] = cap[2];
    30     start.dist = 0;
    31     memset(ans,-1,sizeof(ans));
    32     memset(vis,false,sizeof(vis));
    33     priority_queue<Node> que;
    34     que.push(start);
    35     vis[0][0] = true;
    36     while(!que.empty()){
    37         Node top = que.top();que.pop();
    38         update_ans(top);
    39         if(ans[d] >= 0) break;
    40         for(int i = 0;i < 3;i++){
    41             for(int j = 0;j < 3;j++){
    42                 if(i == j) continue;
    43                 if(top.v[i]==0 || top.v[j]==cap[j]) continue;
    44                 int amount = min(top.v[i],cap[j]-top.v[j]);
    45                 Node Next = top;
    46                 Next.dist += amount;
    47                 Next.v[i] -= amount,Next.v[j] += amount;
    48                 if(!vis[Next.v[0]][Next.v[1]]){
    49                     vis[Next.v[0]][Next.v[1]] = true;
    50                     que.push(Next);
    51                 }
    52             }
    53         }
    54     }
    55     for(int i = d;i >= 0;i--){
    56         if(ans[i] >= 0){
    57             printf("%d %d
    ",ans[i],i);
    58             return;
    59         }
    60     }
    61 }
    62 
    63 int main()
    64 {
    65     //freopen("input.txt","r",stdin);
    66     //freopen("output.txt","w",stdout);
    67     int iCase;
    68     scanf("%d",&iCase);
    69     while(iCase--){
    70         for(int i = 0;i < 3;i++){
    71             scanf("%d",&cap[i]);
    72         }
    73         scanf("%d",&d);
    74         bfs();
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    postgres column reference "id" is ambiguous
    网络 内网穿透frp
    odoo12 支付宝在线支付
    odoo 账号登录不上,重置密码也不管用
    odoo 取消保存提示
    聊聊redis分布式锁的8大坑 转载
    用 Keepalived+HAProxy 实现高可用负载均衡的配置方法 转载
    Nginx+keepalived 实现高可用,常用防盗链及动静分离配置 转载
    Git 实用技巧记录 转载:https://mp.weixin.qq.com/s/o6FvGfiG9b57xTeXlBzzQQ
    5 个冷门但非常实用的 Kubectl 使用技巧,99% 的人都不知道 https://mp.weixin.qq.com/s/h4_KRmsVSnlqCmIJh0altA
  • 原文地址:https://www.cnblogs.com/npugen/p/9539061.html
Copyright © 2011-2022 走看看