HDU1050 Moving Tables

来源:互联网 发布:查看网络连接没反应 编辑:程序博客网 时间:2024/06/11 19:44

Moving Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25394    Accepted Submission(s): 8408


Problem Description
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 



The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving. 



For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.
 

Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.
 

Output
The output should contain the minimum time in minutes to complete the moving, one per line.
 

Sample Input
3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50
 

Sample Output
102030
 

题目大意:就是著名的ACM公司在一栋写字楼租了一层,然后这一层房间的构造如题目所给的图示,分南北两边,每一边都有200个房间,中间有一条过道;现在要在移动一些房间里面的办公桌,办公桌很大,但是走廊过道很窄每次只能通过一张桌子。每一次将办公桌从 i 房间移动到 j 房间都需要10分钟,那么公司想要在最短的时间内将这些桌子移动到房间,并且只要移动时没有公共过道的部分都可以同时进行;总经理给出了能够移动桌子的情况和不能移动桌子的情况;如下:


也就是说 将桌子从30号房间到50号房间移动的时候可以同时将60号的桌子移动到90号,因为它们没有占用共同的过道;

而当将桌子从20号房间移动到40号房间的时候就不能同时将31号房间的桌子移动到80号房间了  因为他们中间有共同的过道,当第一张桌子移动的时候走廊上个是没有空间移动其他桌子的了。


输入第一行有个T表示接下来有T次的测试数据。然后接下来有N,表示有N张桌子需要移动;接下里有N行,每一行有一组 i 和 j 表示从i 号房间移动到 j 号房间。


省题:由题意可知,我们要求的是最少需要花费多少时间移动N张桌子。每次移动的时候如果存在走廊被占用的时候就不能同时移动,如果没有同时被占用就可以同时移动这几张桌子。那么我们可以把它理解成每次移动的时候在 i 到 j 的房间过道上画线,然后找到从复最多的次数的房间乘以移动一次桌子所需要花费的时间就是我们移动N张桌子所需要花费的最少时间;


思路:我们可以每次给数组加一来模拟画线的过程,所以定义一个整型数组count[406],每次有桌子从 i 号房间移动到 j 号房间就将数组所对应的位置加一,最后对数组进行降序排序,得到的 第一个位置的count[0]就是重复次数对多的房间,那么我们将其count[0]*10输出就得到移动N张桌子所需要花费的最少时间;

还有个地方要注意:我给的代码,是直接将南北两边的房间合在和一起,因为他们是公用一条走廊的,为了方便程序的编写,就直接将其处理在一起了;

给出代码:




1 0
原创粉丝点击