程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA - 10588 Queuing at the doctors (隊列)

UVA - 10588 Queuing at the doctors (隊列)

編輯:C++入門知識

UVA - 10588 Queuing at the doctors (隊列)


Description

Download as PDF

Problem H: Queuing at the doctors

\ Due to the increasing number of weird viruses spreading around, all the members of the International Confederation of Revolver Enthusiasts (ICORE) are required by their boss to do quarterly physical checkups at General Hospital. All checkups are arranged by the boss and scheduled on the same day. Each member of ICORE gets instructions where they are given their number from the set {1 ... n} the time of the day when they are supposed to show up at General Hospital a list of doctZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcnM=" offices that they are to visit in the listed order. Doctors' offices in General Hospital are numbered with numbers from the set {1 ... m}.

All the members of ICORE have been convinced that the schedule of the checkups has been professionally prepared and that there would be no lining up and waiting at the doctors' doors. However, since their boss was a political appointment their hopes for not wasting time had to be abandoned as soon as they started arriving at the hospital. The queues were forming rapidly despite the fact that the doctors were very efficient due to their usual sloppiness. The members of ICORE are all very disciplined and obey the following rules for visiting the doctors

if an ICORE member was supposed to show up at the hospital at time t, then at time t they show up at the first doctors' office on their list; if several people show up a doctor's office at time t then they form a queue in increasing order of their numbers and join the end of the queue already formed by people who arrived earlier; if at time t in front of office x there is a queue of people who arrived earlier or at time t, then the first person from the queue enters office x. This person after a time unit (the doctors do a sloppy job, remember) exits the office and at time t+1 appears at the next office from their list of offices to visit. At that time the first person from the queue enters office x; if a visit at office x at time t was for the given visitor the last visit on their list, then at time t+1 this visitor leaves the hospital.

Your task is to find the time when the last visitor leaves the hospital.

The first line of input contains a natural number c giving the number of cases to handle. The following lines form the input for the c cases, each in the format described below. The first line of data for a case contains two natural numbers n and m, 1 ≤ n, m ≤ 1000, giving the number of the visitors and the number of doctors' offices for the case. Each of the following n lines contains a sequence of natural numbers. Among these lines, line i (1 ≤ in) has the following format

t k g1 g2... gk
meaning that the ith visitor arrives at time t and has to visit k offices in the order given by g1g2 ... gk where each gj is a number of doctor's office, 1 ≤ gjm. We have that 0 ≤ t ≤ 1000000 and there is no more than 1000000 visits scheduled for a day at the hospital.

For each of the c input cases print one line giving the time when the last visitor leaves the hospital.





Sample input

 
2
5 3
1 3 3 2 1
0 7 2 3 1 1 1 1 2
2 1 1
1 2 3 3
4 3 1 1 1
5 10
3 1 6
2 3 3 2 8
2 1 4
2 4 7 9 9 6
0 2 8 7

Output for sample input

12
6
題意:某公司要求每個員工都必須到當地的醫院體檢,並給每個員工安排了體檢的順序。為了節約等待時間,員工們被要求分時段去體檢,但排隊仍然是必不可少的。因此,公司制定了下面幾條規定:
    員工的編號從1到n。員工在規定的時間點上一定准時到達醫院開始體檢。員工有自己的體檢順序,並且一定按順序來體檢,直到體檢完才離開醫院。當有多個員工在同一個時刻到同一個醫生那體檢時,編號小的優先,其他人按到達的先後順序和編號大小排隊等待。

    已經知道每個醫生在每單位1的時間內可以檢查一個員工,給定所有員工的體檢時間和體檢順序,請計算一下最後一個員工離開醫院的時間。

    一共有N(1 ≤ N ≤ 1000)個員工,M(1 ≤ M ≤ 1000)個醫生,所有人總的檢查次數不超過1000000次。

    思路:每個項目都用一個優先隊列模擬,處理一個人後,就將這個人丟到它的下一項目對應的隊列中
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    const int maxn = 1100;
    
    struct node {
    	int t, id;
    	bool operator <(const node &a) const {
    		if (t == a.t)
    			return id > a.id;
    		return t > a.t;
    	}
    };
    int n, m;
    priority_queue p[maxn];
    queue q[maxn];
    
    int main() {
    	int t;
    	scanf("%d", &t);
    	while (t--) {
    		scanf("%d%d", &n, &m);
    		int x, num, a;
    		node now;
    		for (int i = 0; i < n; i++) {
    			now.id = i;
    			scanf("%d%d", &x, &num);
    			while (num--) {
    				scanf("%d", &a);
    				a--;
    				q[i].push(a);
    			}
    			now.t = x;
    			p[q[i].front()].push(now);
    		}
    
    		int ans = 0, flag = 1;
    		while (flag) {
    			flag = 0;
    			for (int i = 0; i < m; i++) {
    				if (!p[i].empty()) {
    					flag = 1;
    					now = p[i].top();
    					if (now.t > ans)
    						continue;
    					p[i].pop();
    					q[now.id].pop();
    					if (!q[now.id].empty()) {
    						now.t = ans + 1;
    						p[q[now.id].front()].push(now);
    					}
    				}
    			}
    			ans++;
    		}	
    		printf("%d\n", ans-1);
    	}
    	return 0;
    }


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved