Ultraviolet A 10588-queuing at the doctors (priority queue)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10588-queuing at the doctors

The company arranges employees to perform health checks. A total of N people and M projects, given the time each employee arrives at the hospital, as well as the items and sequence for the health check, each project inspection of one person requires a unit of time. Doctors in each project perform health checks first to employees, and at the same time, employees who receive health checks are given priority to employees with small employee numbers. Ask the last employee to leave the hospital.

Solution: Create a priority queue for each project, traverse the time, perform a health check for each meeting time, and then place the person in the next project corresponding to the person.

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int maxn = 1005;struct item {    int ti, pos, id;    item (int ti = 0, int id = 0, int pos = 0) {        this->ti = ti;        this->id = id;        this->pos = pos;    }    bool operator < (const item& u) const {        return ti > u.ti || (ti == u.ti && id > u.id);    }};int N, M;vector<int> g[maxn];priority_queue<item> que[maxn];void init () {    int t, k, x;    scanf("%d%d", &N, &M);    for (int i = 1; i <= N; i++) {        g[i].clear();        scanf("%d%d", &t, &k);        for (int j = 0; j < k; j++) {            scanf("%d", &x);            g[i].push_back(x);        }        que[g[i][0]].push(item(t, i, 0));    }}int solve () {    int ret = 0;    bool flag = true;    while (flag) {        flag = false;        for (int i = 1; i <= M; i++) {            if (que[i].empty())                continue;            flag = true;            item u = que[i].top();            if (u.ti > ret)                continue;            que[i].pop();            if (u.pos + 1 >= g[u.id].size())                continue;            int next = g[u.id][u.pos+1];            que[next].push(item(ret+1, u.id, u.pos+1));        }        ret++;    }    return ret - 1;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        init();        printf("%d\n", solve());    }    return 0;}

Ultraviolet A 10588-queuing at the doctors (priority queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.