最佳调度问题
2015-12-05 10:21
323 查看
#include <iostream> #include <fstream> #include <queue> #include <cmath> using namespace std; const int INF = 100000; const int MAX = 50; int n, k; //任务数,机器数 int time[MAX]; //完成任务需要时间 class Node { public: int dep; //当前层 int time; //完成当前任务的时间 int *machine; //各机器完成任务后的时间 Node(int d, int t) { machine = new int[k+1]; dep = d; time = t; } int getTime() //计算完成当前任务需要的时间 { int t = machine[1]; for(int i=2; i<=k; i++) if(t < machine[i]) t = machine[i]; return t; } //完成任务时间小的先出队列 bool operator < (const Node &node) const { return time >= node.time; } }; int search() { priority_queue<Node> q; Node enode(0, 0); for(int j=1; j<=k; j++) enode.machine[j] = 0; int best = INF; while(true) { if(enode.dep == n) { if(enode.time < best) { best = enode.time; break; } } else { for(int i=1; i<=k; i++) { Node now(enode.dep+1, 0); copy(enode.machine, enode.machine+k+1, now.machine); now.machine[i] += time[now.dep]; now.time = now.getTime(); if(now.time < best) q.push(now); } } if(q.empty()) break; else { enode = q.top(); cout << enode.dep << " " << enode.time << endl; q.pop(); } } return best; } int main() { ifstream fin("最佳调度.txt"); cout << "输入任务数:"; fin >> n; cout << n << endl; cout << "输入机器数:"; fin >> k; cout << k << endl; cout << "输入完成任务需要的时间:\n"; for(int i=1; i<=n; i++) { fin >> time[i]; cout << time[i] << " "; } cout << "\n完成全部任务最早时间为:" << search() << endl; cout << endl; cout << endl; fin.close(); return 0; }
相关文章推荐
- 如何在高并发分布式系统中生成全局唯一Id
- 在HTML中用Javascript接收参数
- 黑马程序员——C基础---数组
- CLRS 10.4有根树的表示
- 安装Hadoop系列 — 安装Eclipse
- CCBPM高级开发之类设计与数据库设计命名规则
- ADO_NET 数据库连接字符串大全
- 布线问题
- CCBPM关于工作流引擎授权功能说明
- [LeetCode] Largest Rectangle in Histogram 解题思路
- AngularJs Cookies 操作
- 命名空间比较好的博客
- 圆排列问题
- js精度丢失解决办法
- maven项目配置Project Facets时further configuration available不出来问题
- js精度丢失解决办法
- 安装Hadoop系列 — 安装Hadoop
- 第14周项目1-(3)验证二叉排序树相关算法
- n皇后问题
- 迄今为止最浮夸的代码编辑器插件