码迷,mamicode.com
首页 > 其他好文 > 详细

B - Fill

时间:2015-03-11 19:27:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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\n",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 }

 

B - Fill

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4330524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!