[itint5]任务调度 - 阿牧遥 - 博客园
拓扑排序。首先按照题目给出的数据结构复杂度不会是O(v+e)的,所以先要变换数据结构。二来写的时候用一个stack会更好点。还有就是题目里其实ID就是1到n
https://github.com/AnnieKim/ITint5/blob/master/010_%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6.cpp
有n个任务需要完成(编号1到n),任务之间有一些依赖关系,如果任务a依赖于任务b和c,
那么只有当任务b和任务c完成之后才能完成任务a。给定所有的依赖关系,判断这些任务是否能够完成。
如果能够完成,请给出一个合法的任务完成序列。
样例:
n=5
1->2,3
3->4
上述样例中任务1依赖于任务2和任务3,任务3依赖于任务4,那么存在合法的任务完成序列4,3,2,1,5
typedef int JobID;
typedef map<JobID, vector<JobID> > MAP;
typedef MAP::const_iterator Iterator;
/*
* deps[id]表示任务id所依赖的任务
* 如果存在合法的任务完成序列,返回true,否则返回false
* 合法的任务序列请存放在参数result中(已经分配空间,不需要push_back)
*/
bool jobSchedule(const map<JobID, vector<JobID> > &deps, int n, vector<JobID> &result)
{
MAP notify;
queue<JobID> q;
int deps_count[n+1];
for (int i = 1; i <= n; ++i)
deps_count[i] = 0;
for (Iterator it = deps.begin(); it != deps.end(); ++it)
{
JobID id = it->first;
const vector<JobID> &jobs = it->second;
int N = jobs.size();
deps_count[id] = N;
for (int i = 0; i < N; ++i)
notify[jobs[i]].push_back(id);
}
for (int i = 1; i <= n; ++i)
if (deps_count[i] == 0)
q.push(i);
int index = 0;
while (!q.empty())
{
JobID id = q.front(); q.pop();
result[index++] = id;
if (notify.find(id) == notify.end())
continue;
vector<JobID> &jobs = notify[id];
for (int i = 0; i < jobs.size(); ++i)
{
deps_count[jobs[i]]--;
if (deps_count[jobs[i]] == 0)
q.push(jobs[i]);
}
}
return index == n;
}
https://github.com/zdzapple/itint5/blob/master/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6.cpp
typedef int JobID;
/* deps[id]表示任务id所依赖的任务
* 如果存在合法的任务完成序列,返回true,否则返回false
* 合法的任务序列请存放在参数result中(已经分配空间,不需要push_back)
*/
bool jobSchedule(const map<JobID, vector<JobID> > &deps, int n,
vector<JobID> &result)
{
vector<int> indegree(n + 1, 0);
map<JobID, vector<JobID> > rmap;
for (map<JobID, vector<JobID> >::const_iterator it = deps.begin();
it != deps.end(); ++ it)
{
vector<JobID> vec = it->second;
for (int i = 0; i < vec.size(); ++ i)
{
if (rmap.find(vec[i]) == rmap.end()) {
vector<int> postJobs;
rmap[vec[i]] = postJobs;
}
rmap[vec[i]].push_back(it->first);
indegree[it->first] ++;
}
}
stack<int> preJobs;
for (int i = 1; i <= n; ++ i)
if (indegree[i] == 0)
preJobs.push(i);
int index = 0;
for (int i = 0; i < n; ++ i)
{
if (preJobs.empty())
return false;
int preJob = preJobs.top();
result[index ++] = preJob;
preJobs.pop();
if (rmap.find(preJob) == rmap.end())
continue;
vector<int> postJobs = rmap[preJob];
for (int t = 0; t < postJobs.size(); ++ t)
{
indegree[postJobs[t]] --;
if (indegree[postJobs[t]] == 0) {
preJobs.push(postJobs[t]);
}
}
}
return true;
}
Read full article from [itint5]任务调度 - 阿牧遥 - 博客园
拓扑排序。首先按照题目给出的数据结构复杂度不会是O(v+e)的,所以先要变换数据结构。二来写的时候用一个stack会更好点。还有就是题目里其实ID就是1到n
typedef
int
JobID;
/*
* deps[id]表示任务id所依赖的任务
* 如果存在合法的任务完成序列,返回true,否则返回false
* 合法的任务序列请存放在参数result中(已经分配空间,不需要push_back)
*/
/*
id are from 1 to n
*/
bool
jobSchedule(
const
map<JobID, vector<JobID> > &deps,
int
n,
vector<JobID> &result) {
map<JobID, vector<JobID>> rmap;
vector<
int
> indeg(n+1, 0);
for
(map<JobID, vector<JobID> >::const_iterator it = deps.begin(); it != deps.end(); it++) {
indeg[it->first] = it->second.size();
for
(
int
i = 0; i < it->second.size(); i++) {
rmap[it->second[i]].push_back(it->first);
}
}
stack<JobID> stak;
for
(
int
i = 1; i <= n; i++) {
if
(indeg[i] == 0) {
stak.push(i);
}
}
for
(
int
i = 0; i < n; i++) {
if
(stak.empty())
return
false
;
JobID id = stak.top();
stak.pop();
result[i] = id;
for
(
int
j = 0; j < rmap[id].size(); j++) {
indeg[rmap[id][j]]--;
if
(indeg[rmap[id][j]] == 0) {
stak.push(rmap[id][j]);
}
}
}
return
true
;
}
有n个任务需要完成(编号1到n),任务之间有一些依赖关系,如果任务a依赖于任务b和c,
那么只有当任务b和任务c完成之后才能完成任务a。给定所有的依赖关系,判断这些任务是否能够完成。
如果能够完成,请给出一个合法的任务完成序列。
样例:
n=5
1->2,3
3->4
上述样例中任务1依赖于任务2和任务3,任务3依赖于任务4,那么存在合法的任务完成序列4,3,2,1,5
typedef int JobID;
typedef map<JobID, vector<JobID> > MAP;
typedef MAP::const_iterator Iterator;
/*
* deps[id]表示任务id所依赖的任务
* 如果存在合法的任务完成序列,返回true,否则返回false
* 合法的任务序列请存放在参数result中(已经分配空间,不需要push_back)
*/
bool jobSchedule(const map<JobID, vector<JobID> > &deps, int n, vector<JobID> &result)
{
MAP notify;
queue<JobID> q;
int deps_count[n+1];
for (int i = 1; i <= n; ++i)
deps_count[i] = 0;
for (Iterator it = deps.begin(); it != deps.end(); ++it)
{
JobID id = it->first;
const vector<JobID> &jobs = it->second;
int N = jobs.size();
deps_count[id] = N;
for (int i = 0; i < N; ++i)
notify[jobs[i]].push_back(id);
}
for (int i = 1; i <= n; ++i)
if (deps_count[i] == 0)
q.push(i);
int index = 0;
while (!q.empty())
{
JobID id = q.front(); q.pop();
result[index++] = id;
if (notify.find(id) == notify.end())
continue;
vector<JobID> &jobs = notify[id];
for (int i = 0; i < jobs.size(); ++i)
{
deps_count[jobs[i]]--;
if (deps_count[jobs[i]] == 0)
q.push(jobs[i]);
}
}
return index == n;
}
https://github.com/zdzapple/itint5/blob/master/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6.cpp
typedef int JobID;
/* deps[id]表示任务id所依赖的任务
* 如果存在合法的任务完成序列,返回true,否则返回false
* 合法的任务序列请存放在参数result中(已经分配空间,不需要push_back)
*/
bool jobSchedule(const map<JobID, vector<JobID> > &deps, int n,
vector<JobID> &result)
{
vector<int> indegree(n + 1, 0);
map<JobID, vector<JobID> > rmap;
for (map<JobID, vector<JobID> >::const_iterator it = deps.begin();
it != deps.end(); ++ it)
{
vector<JobID> vec = it->second;
for (int i = 0; i < vec.size(); ++ i)
{
if (rmap.find(vec[i]) == rmap.end()) {
vector<int> postJobs;
rmap[vec[i]] = postJobs;
}
rmap[vec[i]].push_back(it->first);
indegree[it->first] ++;
}
}
stack<int> preJobs;
for (int i = 1; i <= n; ++ i)
if (indegree[i] == 0)
preJobs.push(i);
int index = 0;
for (int i = 0; i < n; ++ i)
{
if (preJobs.empty())
return false;
int preJob = preJobs.top();
result[index ++] = preJob;
preJobs.pop();
if (rmap.find(preJob) == rmap.end())
continue;
vector<int> postJobs = rmap[preJob];
for (int t = 0; t < postJobs.size(); ++ t)
{
indegree[postJobs[t]] --;
if (indegree[postJobs[t]] == 0) {
preJobs.push(postJobs[t]);
}
}
}
return true;
}
Read full article from [itint5]任务调度 - 阿牧遥 - 博客园