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

原创 2005年05月04日 15:31:00

#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

自己实现一个简单的优先队列-二叉堆

自己实现一个简单的优先队列(Priority Queue)-二叉堆(Binary Heap).

【省选模拟试题】排水系统 dijkstra+最短路径树+优先队列维护(当然这个和dij的不是同一个)

题意概述:给出一张N点M边的无向图,问删除一条边之后0->1的最短路长度的最大值是多少,保证删除任意一条边都有0->1的路径存在。N...

hdu 2066 一个人的旅行(优先队列 dijkstr)

Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能...

CSU 1090: Number Transformation 一个非常好的优先队列打表

1090: Number Transformation Time Limit: 1 Sec  Memory Limit: 128 MB SUBMIT: 375  Solved: 41 [SU...

用java构建一个优先队列

package Com.Tree; public class BinaryHeap> { //定义初始化大小 private int currentSize; private final s...

JAVA拾遗 - 优先队列的探讨以及其在KNN算法中的应用

这几天导师安排写一个微博签到数据依据地理坐标的分类,所以有一段时间没有更新博客了,不过正好也在这次代码的写作中学习了一些新的姿势,写这篇博客跟大家分享下心得。 优先队列定义所谓的优先队列,就是在普通队...

POJ 1442 堆的应用 优先队列

点击打开链接                  i  数列序号 1 ADD(3)      0  3 2 GET         1  3                              ...

2830 蓬莱山辉夜 优先队列的简单应用

在幻想乡中,蓬莱山辉夜是月球公主,居住在永远亭上,二次设定说她成天宅在家里玩电脑,亦称NEET姬 一天,她要她帮忙升级月球的网络服务器,应为注册用户过多(月兔和地球上的巫女都注册了……),所以作为代...

数据结构算法与应用第九章-优先队列

第六章讲述了FIFO队列,第八章讲述了二叉树,在本章讲述优先队列。内容包括优先队列的线性表描述(公式描述线性表和链表)、堆、左高树、堆排序问题、机器调度问题和霍夫曼编码问题。   优先队列定义:优...

优先队列及BFS应用

优先队列:顾名思义,首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时,有一定的选择性,即根据元素的属性选择某一项值最优的出队~ 百度百科上...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:我写的一个优先队列的应用
举报原因:
原因补充:

(最多只允许输入30个字)