我写的一个优先队列的应用

#include <stdio.h>
#define TOTAL_MINED 10000

typedef struct {
     int event_begin;
} event_type;

const int mvtime, btime, mine_time, max_quentity, mined_per_time;
int total_time, total_mined;
event_type *event_queue;

void init();
void reading();
void mining();
void writing();

main()
{   
     reading();
     init();
     mining();
     writing();
}

void reading()
{
     printf("btime mvtime mine_time max_quentity mined_per_time:/n");
     scanf("%d%d%d%d%d", &btime, &mvtime, &mine_time, &max_quentity, &mined_per_time);
}
void writing()
{
     printf("total_time:/t%d seconds/n", total_time);
}

void PercolateDown(int i);
void init()
{
     int i;
    
     if (!(event_queue = (event_type *) malloc((max_quentity+1) * sizeof(event_type)))) {
   printf("no memory!");
   exit(1);
     }
     event_queue[0].event_begin = 0;
     for (i = 1; i <= max_quentity; i++)
   event_queue[i].event_begin = i * btime + mvtime;
     for (i = max_quentity / 2; i > 0; i--)
   PercolateDown(i);
}
void IncreaseMin(int i);
void mining()
{
     int last_event_end;
     total_mined = 0;
     while ((total_mined += mined_per_time) < TOTAL_MINED) {
   last_event_end = event_queue[1].event_begin + mine_time;
   IncreaseMin(2 * mvtime + mine_time);
   if (last_event_end > event_queue[1].event_begin)
        event_queue[1].event_begin = last_event_end;
     }
     total_time = event_queue[1].event_begin + mine_time + mvtime;

void PercolateDown(int i)
{
     int child;
     event_type tmp = event_queue[i];
     for ( ; i * 2 <= max_quentity; i = child) {
   /* find smaller child */
   child = i * 2;
   if (child != max_quentity && event_queue[child+1].event_begin < event_queue[child].event_begin)
        child++;
   /* percolate one level */
   if (tmp.event_begin > event_queue[child].event_begin)
        event_queue[i] = event_queue[child];
   else
        break;
     }
     event_queue[i] = tmp;
}
void IncreaseMin(int i)
{
     event_queue[1].event_begin += i;
     PercolateDown(1);
}


题目:

A mining base needs to build some robots to collect at least 10000 units of resource. Each robot will start from the base, reach the diggings in S minutes, work for W minutes, and then take C units of resource back to the base in S minutes.
  To speed up this procedure, K robots will be built at the base. It takes M minutes to produce one robot. A robot will be set to start working immediately after it is built, and producing the next robot will be on line right after. This procedure continues untill all the robots are built.
  Due to the limitation of the mining equipments, there can be only one robot digging at the working area. That is, it is only after the currently working robot finishes its collecting work and starts getting back to the base that the next robot can work at the diggings.
  Now it is your job to write a program to simulate this procedure, and find out how many minutes it will take to collect at least 10000 units of resource.

Input
There are several lines of input. Each line contains a test case which consists of 5 integers, namely S, W, C, K, and M.

Output
For each test case, you are asked to output an integer t, which is the number of minutes taken to collect at least 10000 units of resource.

Sample Input:

10 20 10 1 5
Sample Output:

40005

阅读更多
上一篇管中窥豹------从烟熏看大学
下一篇好生厌恶
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭