贪心算法-活动选择
2012-12-03 22:21
281 查看
问题描述
问题来自算法导论16.1。
几个相互竞争的活动进行调度,他们要求以独占的方式使用某一公共资源。调度的目标是找出一个最大的相互兼容活动集合。
思想:总是选择剩余活动中具有最早结束时间的活动。
参考:
算法导论,第二版,机械工业出版社
算法导论-贪心算法-http://blog.csdn.net/liuzhanchen1987/article/details/7854826
问题来自算法导论16.1。
几个相互竞争的活动进行调度,他们要求以独占的方式使用某一公共资源。调度的目标是找出一个最大的相互兼容活动集合。
思想:总是选择剩余活动中具有最早结束时间的活动。
/***************************************************************************** Copyright: 2012, USTC File name: main.cpp Description:Arrange activities by greedy method. Author:Silang Quan Version: 1.0 Date: 2012.12.3 *****************************************************************************/ #include<iostream> #include<vector> using namespace std; //Iteration Method assume a[1] is the first activity meet the need. int GreedySelect(int *s,int *f,int length,int *a) { int i,j=2; a[1]=1; i=1; for(int m=2;m<=length;m++) { if(s[m]>=f[i]) { a[j++]=m; i=m; } } return j; } //Recursion method void GreedySelect2(int *s,int *f,int i,int j,vector<int>& result)// const int started[], const int finished[],vector<int>& result, int i,int j) { if( i >= j ) return; int l; for( l = i+1; l < j; ++l ) { if( s[l] >= f[i] ) { result.push_back(l); break; } } GreedySelect2(s,f,l,j,result); } int main() { int s[12]={0,1,3,0,5,3,5,6,8,8,2,12}; int f[12]={0,4,5,6,7,8,9,10,11,12,13,14}; //test1 /* int a[13]; int i=GreedySelect(s,f,11,a); for(int j=1;j<i;j++) { cout<<a[j]<<" "; } cout<<endl; */ //test2 vector<int> result; GreedySelect2(s,f,0,12,result); for(vector<int>::iterator iter=result.begin();iter!=result.end();++iter) { cout<<*iter<<" "; } cout<<endl; }
参考:
算法导论,第二版,机械工业出版社
算法导论-贪心算法-http://blog.csdn.net/liuzhanchen1987/article/details/7854826
相关文章推荐
- 活动选择与小船过河问题(贪心算法)
- 算法导论第16章 贪心算法-活动选择问题
- 活动选择问题(动态规划算法和贪心算法)
- 贪心算法-活动选择
- 活动选择(贪心算法) p226
- 贪心算法-活动选择
- 算法导论 第16章 活动选择问题的递归和迭代贪心算法
- 《算法导论》之 贪心算法—活动选择问题
- 贪心算法-活动选择
- 算法导论 第16章 贪心算法-活动选择问题C++实现
- 算法导论-第16章-贪心算法-16.1 活动选择问题
- 贪心算法与活动选择问题和背包问题
- 活动选择问题(活动安排问题)(最大数目活动选择问题)贪心算法C++实现
- 贪心算法-活动选择
- 贪心算法(Greedy Algorithm)之活动选择问题(Activity-Selection Problem)
- 贪心算法与活动选择问题 C++实现
- 贪心算法-活动选择
- 活动选择问题(活动安排问题)(最大数目活动选择问题)贪心算法C++实现
- 贪心算法之活动选择
- 活动选择问题 (动态规划、贪心算法)