问题描述:给定活动集合,包括活动i的开始时间si和结束时间fi,求最大兼容活动子集。
i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
si | 1 | 3 | 0 | 5 | 3 | 5 | 6 | 8 | 8 | 2 | 12 |
fi | 4 | 5 | 6 | 7 | 9 | 9 | 10 | 11 | 12 | 14 | 16 |
问题的解法有三个:动态规划,递归的贪心算法和迭代的贪心算法
下面给出源码:
activitySelector.h
#include<stdio.h>
#include<stdlib.h>
struct res{
int key;
struct res *next;
};
void DActivitySelector(int *s,int *f,int **c,int **r,int n);//动态规划的活动选择算法
void printResult(int **c,int **r,int i,int j);//打印动态规划算法的结果
void recursiveActivitySelector(int *s,int *f,int k,int n,struct res *head);//递归的贪心算法
void printRescursiveResult(struct res *head);//打印递归的贪心算法的结果
void greedyActivitySelector(int *s,int *f,int n,struct res *head);//迭代的贪心算法
void printGreedyResult(struct res *head);//打印迭代的贪心算法的结果
activitySelector.cpp
#include"activitySelector.h"
void DActivitySelector(int *s,int *f,int **c,int **r,int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
c[i][j]=0;
r[i][j]=0;
}
c[i][i]=1;
}
int l;
for(l=3;l<=n;l++){
for(i=0;i<n-l+1;i++){
j=i+l-1;
for(int k=i+1;k<j;k++){
if(s[k]>=f[i]&&f[k]<=s[j]){
int q=c[i][k]+c[k][j]+1;
if(c[i][j]<q){
c[i][j]=q;
r[i][j]=k;
}
}
}
}
}
}
void printResult(int **c,int **r,int i,int j){
if(c[i][j]==0)
return ;
else{
int k=r[i][j];
if(c[i][i]!=0){
printf("a[%d]",i+1);
c[i][i]=0;
}
printResult(c,r,i,k);
printf("a[%d]",k+1);
c[k][k]=0;
printResult(c,r,k,j);
if(c[j][j]!=0){
printf("a[%d]",j+1);
c[j][j]=0;
}
}
}
void recursiveActivitySelector(int *s,int *f,int k,int n,struct res *head){
int m=k+1;
while(m<=n&&s[m]<f[k])
m=m+1;//寻找ak结束之后第一个结束的任务
if(m<=n){
struct res *p;
p=(struct res *)malloc(sizeof(struct res));
p->key=m;
p->next=NULL;
head->next=p;
recursiveActivitySelector(s,f,m,n,p);
}
else{
head->next=NULL;
}
}
void printRescursiveResult(struct res *head){
struct res *p;
p=head->next;
while(p!=NULL){
printf("a[%d]",p->key);
p=p->next;
}
}
void greedyActivitySelector(int *s,int *f,int n,struct res *head){
head->key=0;
int k=0;
struct res *p;
p=head;
for(int m=1;m<n;m++){
if(s[m]>=f[k]){
struct res *q;
q=(struct res *)malloc(sizeof(struct res));
q->key=m;
q->next=NULL;
p->next=q;
p=p->next;
k=m;
}
}
}
void printGreedyResult(struct res *head){
struct res *p;
p=head;
while(p!=NULL){
printf("a[%d]",p->key+1);
p=p->next;
}
}
main.cpp
#include"activitySelector.h"
int main(){
int n=11;
int s1[11]={1,3,0,5,3,5,6,8,8,2,12},f1[11]={4,5,6,7,9,9,10,11,12,14,16};
int **c,**r;
c=(int **)malloc(sizeof(int *)*11);
r=(int **)malloc(sizeof(int *)*11);
int i;
for(i=0;i<11;i++){
c[i]=(int *)malloc(sizeof(int)*11);
r[i]=(int *)malloc(sizeof(int)*11);
}
DActivitySelector(s1,f1,c,r,11);
printResult(c,r,0,10);
//
printf("\n");
int s2[12]={0,1,3,0,5,3,5,6,8,8,2,12},f2[12]={0,4,5,6,7,9,9,10,11,12,14,16};
struct res *head;
head=(struct res *)malloc(sizeof(struct res));
head->key=0;
head->next=NULL;
recursiveActivitySelector(s2,f2,0,12,head);
printRescursiveResult(head);
//
printf("\n");
struct res *head1;
head1=(struct res *)malloc(sizeof(struct res));
head1->key=0;
head1->next=NULL;
greedyActivitySelector(s1,f1,11,head1);//迭代的贪心算法
printGreedyResult(head1);//打印迭代的贪心算法的结果
system("pause");
return 0;
}