ZOJ Problem Set - 1200 Mining 【优先队列】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/87972382

题目链接

题目大意:一个采矿基地需要建造一些机器人来收集至少10000单位的资源。每个机器人从基地出发,在S分钟内到达挖掘地点,工作W分钟,然后在S分钟内将C个单位的资源运回基地。

为了加快这一进程,将在基地建造K个机器人。制造一个机器人需要M分钟。机器人建成后将立即开始工作,生产下一个机器人后将立即上线。这个过程一直持续到所有的机器人都被制造出来。

由于采矿设备的限制,只能有一个机器人在工作区域进行挖掘。也就是说,只有当当前工作的机器人完成采集工作并开始返回基地后,下一个机器人才能在挖掘现场工作。

现在您的工作是编写一个程序来模拟这个过程,并找出收集至少10,000个资源单元需要多少分钟。

解题思路:计算采集至少10000个资源单位需要机器人工作的总次数n,利用优先队列,首先把每个机器人第一次到达工作区域需要的时间放入到优先队列中,然后取出花费时间最少的机器人,让它进行采集工作,并把它下一次到达工作区域需要的时间放入到优先队列中,如此重复进行n次即可。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;
priority_queue <int, vector<int>, greater<int> > q;
int s,w,c,k,m;
int main()
{
    while(scanf("%d%d%d%d%d",&s,&w,&c,&k,&m)!=EOF)
    {
        while(!q.empty())
        {
            q.pop();
        }
        int n=9999/c+1;//需要的机器人工作的总次数
        for(int i=1;i<=k;i++)
            q.push(i*m+s);//生产的第i个机器人到达工作区域需要的时间
        int time=q.top();//time表示当前一个机器人进入工作区域的时间
        for(int i=1;i<n;i++)
        {
            q.pop();
            q.push(time+s*2+w);
            if(q.top()-time<w)
                time+=w;
            else time=q.top();
        }
        time+=s+w;
        printf("%d\n",time);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/87972382