知识点:
http://blog.youkuaiyun.com/u014488381/article/details/41517359
http://www.cnblogs.com/skywang12345/archive/2014/05/14/3711489.html
例题:
/*******
http://blog.youkuaiyun.com/u014488381/article/details/41517359
http://www.cnblogs.com/skywang12345/archive/2014/05/14/3711489.html
例题:
/*******
5-34 任务调度的合理性 (25分)
假定一个工程项目由一组子任务构成,子
任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。
比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同时开设,因为它们有先后的依赖关系,比如C程序设计和数据结构两门课,必须先学习前者。
但是需要注意的是,对一组子任务,并不是任意的任务调度都是一个可行的方案。比如方案中存在“子任务A依赖于子任务B,子任务B依赖于子任务C,子任务C又依赖于子任务A”,那么这三个任务哪个都不能先执行,这就是一个不可行的方案。你现在的工作是写程序判定任何一个给定的任务调度是否可行。
输入格式:
输入说明:输入第一行给出子任务数NNN(≤100\le 100≤100),子任务按1~NNN编号。随后NNN行,每行给出一个子任务的依赖集合:首先给出依赖集合中的子任务数KKK,随后给出KKK个子任务编号,整数之间都用空格分隔。
输出格式:
如果方案可行,则输出1,否则输出0。
输入样例1:
12
0
0
2 1 2
0
1 4
1 5
2 3 6
1 3
2 7 8
1 7
1 10
1 7
输出样例1:
1
输入样例2:
5
1 4
2 1 4
2 2 5
1 3
0
输出样例2:
0
************/
方法一:效率低,耗时。
1)数组存法:
#include <iostream>
方法一:效率低,耗时。
1)数组存法:
#include <iostream>
#include<string.h>
using namespace std;
int R_store[105][105],C_store[105][105],N,K;
int main()
{
int Mark[105];
memset(C_store,0,sizeof(C_store));
memset(Mark,0,sizeof(Mark));
cin>>N;
for(int i=1;i<=N;i++)
{
int M;
cin>>K;
R_store[i][0]=K;
for(int r=1;r<=K;r++)
{
cin>>M;
R_store[i][r]=M;
int J=C_store[M][0]+1;
C_store[M][0]=C_store[M][0]+1;
C_store[M][J]=i;
}
}
int t;
for( t=1;t<=N;t++)
{
int V=-1;
for(int r=1;r<=N;r++)
{
if
(!Mark[r]&&R_store[r][0]==0)//标记可以不用Mark,可以把stor[r][0]=-1;
{
// cout<<r<<" r"<<endl;
V=r;
Mark[r]=1;
break;
}
}
if(V==-1)
break;
for(int r=1;r<=C_store[V][0];r++)
{
int U=C_store[V][r];
R_store[U][0]=R_store[U][0]-1;
}
}
if(t<N)
cout<<"0"<<endl;
else
cout<<"1"<<endl;
return 0;
}
2)链表法:
方法二:使用了队列,算法效率提高。
方法三:自创DFS搜索,利用回溯;
2)链表法:
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<string.h>
using namespace std;
struct C_vexstruct
{
int data;
C_vexstruct *next;
};
struct Vexstruct
{
int data;
C_vexstruct *head;
};
Vexstruct Vex[105];
int N,A[105],top[105];
void insert_C(int I,int M)
{
C_vexstruct *D=(C_vexstruct*)malloc(sizeof(C_vexstruct));
D->data=M;
D->next=NULL;
if(Vex[I].head)
{
C_vexstruct *last;
last=Vex[I].head;
while(last->next)
{
last=last->next;
}
last->next=D;
}
else
{
Vex[I].head=D;
}
}
int main()
{
while(cin>>N)
{
int K,M;
C_vexstruct *last;
for(int i=1; i<=N; i++)
{
cin>>K;
A[i]=K;///--------入度
for(int r=1; r<=K; r++)
{
cin>>M;
insert_C(M,i);
}
}
///-------topsort
int tim=1;
int Mark[105];
memset(Mark,0,sizeof(Mark));
for( tim=1; tim<=N; tim++)
{
int V=-1;
for(int r=1; r<=N; r++)
{
if(!Mark[r]&&A[r]==0)
{
Mark[r]=1;
V=r;
break;
}
}
//cout<<V<<endl;
if(V==-1)
break;
last=Vex[V].head;
while(last)
{
int D;
D=last->data;
A[D]=A[D]-1;
last=last->next;
}
}
// cout<<tim<<"*"<<endl;
if(tim>=N)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
方法二:使用了队列,算法效率提高。
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
using namespace std;
struct C_vexstruct{
int data;
C_vexstruct *next;
};
struct Vexstruct{
int data;
C_vexstruct *head;
};
Vexstruct Vex[105];
int N,A[105],top[105];
void insert_C(int I,int M)
{
C_vexstruct *D=(C_vexstruct*)malloc(sizeof(C_vexstruct));
D->data=M;
D->next=NULL;
if(Vex[I].head)
{
C_vexstruct *last;
last=Vex[I].head;
while(last->next)
{
last=last->next;
}
last->next=D;
}else
{
Vex[I].head=D;
}
}
int main()
{
while(cin>>N)
{
/*
for(int i=1;i<=N;i++)
{
Vex[i].head=NULL;
}*/
int K,M;
C_vexstruct *last;
for(int i=1;i<=N;i++)
{
cin>>K;
A[i]=K;///--------入度
for(int r=1;r<=K;r++)
{
cin>>M;
insert_C(M,i);
}
}
///-------topsort
queue<int>q;
for(int i=1;i<=N;i++)
{
if(A[i]==0)
{
q.push(i);
}
}
int tim=1;
while(!q.empty())
{
int V=q.front();
q.pop();
top[tim]=V;
tim=tim+1;
last=Vex[V].head;
while(last)
{
int D;
D=last->data;
A[D]=A[D]-1;
if(A[D]==0)
{
q.push(D);
}
last=last->next;
}
}
if(tim==N+1)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
方法三:自创DFS搜索,利用回溯;
#include <iostream>
#define MAX 1000
#include<string.h>
using namespace std;
int stroe[MAX][MAX];
int stroesum[MAX];
int Mark[MAX];
int N,B;
void DFS(int I)
{
if(B==0)
{
return;
}
if(Mark[I]==1)
{
return;
}else
{
if(Mark[I]==-1)
{
//cout<<I<<" * "<<endl;
B=0;
return;
}
Mark[I]=-1;
int M=stroesum[I];
for(int i=1;i<=M;i++)
{
int V;
V=stroe[I][i];
DFS(V);
}
Mark[I]=1;
}
}
int main()
{
while(cin>>N)
{
B=1;
//memset(stroe,0,sizeof(stroe));
memset(Mark,0,sizeof(Mark));
for(int i=1;i<=N;i++)
{
int M;
cin>>M;
if(M==0)
Mark[i]=1;
stroesum[i]=M;
for(int r=1;r<=M;r++)
{
cin>>stroe[i][r];
}
}
for(int i=1;i<=N;i++)
{
if(Mark[i]==0)
{
DFS(i);
}
}
if(B)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}