zoukankan      html  css  js  c++  java
  • UVA10603 倒水问题 Fill

    伫倚危楼风细细,望极春愁,黯黯生天际。草色烟光残照里,无言谁会凭阑意。
    拟把疏狂图一醉,对酒当歌,强乐还无味。衣带渐宽终不悔,为伊消得人憔悴。——柳永

    题目:倒水问题

    网址:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=18&page=show_problem&problem=1544

    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 Output

    2 2
    9859 62
    

    这道题可以使用优先队列BFS解决。

    不难清楚,状态就是(a,b,c)代表三个容器中的当前水量(注意:顺序有差别)。

    状态就是(a,b,c),直接跑一遍BFS即可。

    核心在于,该状态记录起来过于繁琐,如何简化状态?
    显然:总水量是定值,那么只要前两个容器水量确定,就可以刻画出了整个状态!所以状态数组仅需二维即可。该状态下需要判重。
    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
    
    const int maxn = 200 + 5;
    struct node
    {
    	int dist, cup[3];
    	bool operator < (const node& rhs) const
    	{
    		return dist > rhs.dist;
    	}
    };
    int cap[3], d, dis[maxn][maxn];
    bool vis[maxn][maxn];
    void bfs()
    {
    	int ans = 0, num = 0x7f;
    	priority_queue <node> Q;
    	while(!Q.empty()) Q.pop();
    	if(cap[2] <= d)
    	{
    		printf("0 %d
    ", cap[2]);
    		return;
    	}
    	memset(dis, 0x7f, sizeof(dis));
    	memset(vis, false, sizeof(vis));
    	node start;
    	start.dist = 0, start.cup[0] = 0, start.cup[1] = 0, start.cup[2] = cap[2];
    	Q.push(start);
    	dis[0][0] = 0;
    	vis[0][0] = true;
    	int v[3], count;
    	while(!Q.empty())
    	{
    		node now = Q.top(), next;
    		Q.pop();
    		v[0] = now.cup[0], v[1] = now.cup[1], v[2] = now.cup[2];
    		if(v[0] == d || v[1] == d || v[2] == d)
    		{
    			printf("%d %d
    ", dis[now.cup[0]][now.cup[1]], d);
    			return;
    		}
    		for(int i = 0; i < 3; ++ i)
    		{
    			if(v[i] < d && ans < v[i])
    			{
    				ans = v[i];
    				num = now.dist;
    			}
    			if(v[i] == ans) num = min(num, now.dist);
    		}
    
    		for(int i = 0; i < 3; ++ i)
    		{
    			if(now.cup[i])
    				for(int j = 0; j < 3; ++ j)
    				{
    					if(j != i)
    					{
    						if(now.cup[i] + now.cup[j] > cap[j]) count = cap[j] - now.cup[j];
    						else count = now.cup[i];
    						now.cup[i] -= count, now.cup[j] += count;
    						if(dis[now.cup[0]][now.cup[1]] - count >= dis[v[0]][v[1]])
    						{
    							dis[now.cup[0]][now.cup[1]] = dis[v[0]][v[1]] + count;
    							if(!vis[now.cup[0]][now.cup[1]])
    							{
    								vis[now.cup[0]][now.cup[1]] = true;
    								Q.push((node)
    								{
    									dis[now.cup[0]][now.cup[1]], now.cup[0], now.cup[1], now.cup[2]
    								});
    							}
    						}
    						now.cup[i] += count, now.cup[j] -= count;
    					}
    				}
    		}
    	}
    	printf("%d %d
    ", num, ans);
    	return;
    }
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T --)
    	{
    		for(int i = 0; i < 3; ++ i)
    			scanf("%d", &cap[i]);
    		scanf("%d", &d);
    		bfs();
    	}
    	return 0;
    }
    

    该道题启示我们可以发掘一些隐含的条件来简化状态,从而简化时间及空间的复杂度。

  • 相关阅读:
    HashSet,TreeSet和LinkedHashSet
    HashMap结构及使用
    Elasticsearch-如何控制存储和索引文档(_source、_all、返回源文档的某些字段)
    Elasticsearch-数组和多字段
    Elasticsearch-布尔类型
    Elasticsearch-日期类型
    Elasticsearch-数值类型
    Elasticsearch-字符串类型
    Elasticsearch-使用映射来定义各种文档
    Elasticsearch-集群增加节点
  • 原文地址:https://www.cnblogs.com/zach20040914/p/12810845.html
Copyright © 2011-2022 走看看