• 数据结构与算法题目集(中文)7-34 任务调度的合理性 (25分) 拓扑排序(典型)


    1.题目

    假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。

    比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同时开设,因为它们有先后的依赖关系,比如C程序设计和数据结构两门课,必须先学习前者。

    但是需要注意的是,对一组子任务,并不是任意的任务调度都是一个可行的方案。比如方案中存在“子任务A依赖于子任务B,子任务B依赖于子任务C,子任务C又依赖于子任务A”,那么这三个任务哪个都不能先执行,这就是一个不可行的方案。你现在的工作是写程序判定任何一个给定的任务调度是否可行。

    输入格式:

    输入说明:输入第一行给出子任务数N(≤100),子任务按1~N编号。随后N行,每行给出一个子任务的依赖集合:首先给出依赖集合中的子任务数K,随后给出K个子任务编号,整数之间都用空格分隔。

    输出格式:

    如果方案可行,则输出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

    2.题目分析

    能完成拓扑排序集为符合要求

    注:拓扑排序不一定只有一条路径(https://www.cnblogs.com/bigsai/p/11489260.html

    注意拓扑排序的代码实现

    3.代码

    /*#include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    using namespace std;
    #define INF 100000
    #define maxx 103
    typedef struct
    {
    	int n, m;
    	int edges[maxx][maxx];
    }MGraph;
    int in[maxx];
    int process(MGraph g)
    {
    	queue<int> list;
    	int amount = 0;
    	for (int i = 1; i <= g.n; i++)
    	{
    		if (in[i] == 0)
    			list.push(i);
    	}
    	while (!list.empty())
    	{
    		int v = list.front();
    		amount++;
    		list.pop();
    		for (int i = 1; i <= g.n; i++)
    		{
    			if (g.edges[v][i] != INF)
    			{
    				if (--in[i] == 0)list.push(i);
    			}
    		}
    
    	}
    
    	if (amount == g.n)return 0;
    	else
    		return 1;
    		
    
    }
    
    int  main()
    {
    	MGraph g;
    	cin >> g.n;
    	memset(in, 0, sizeof(in));
    	for (int i = 0; i < maxx; i++)
    	{
    		for (int j = 0; j < maxx; j++)
    		{
    			g.edges[i][j] = INF;
    		}
    	}
    	for (int i = 1; i <= g.n; i++)
    	{
    		int  temp;
    		cin >> temp;
    		for (int j = 0; j < temp; j++)
    		{
    			int temp2;
    			cin >> temp2;
    			g.edges[i][temp2] =1;
    			in[temp2]++;
    		}
    	}
    	if (process(g) != 0)cout << "0" << endl;
    	else
    	{
    	
    		cout << "1" << endl;
    	}
    
    
    }
    */
    #include<iostream>
    #include<queue>
    using namespace std;
    #define max 200
    #define INF 1000000
    int egdes[max][max];
    int in[max] = {0};
    
    void process(int n)
    {
    	int cnt = 0;
    	queue<int>qu;
    	for (int i = 1; i <=n; i++)
    	{
    		if (in[i] == 0)
    			qu.push(i);
    	}
    
    	while (!qu.empty())
    	{
    		int  temp = qu.front();
    		qu.pop();
    		cnt++;
    		for (int i = 1; i <=n; i++)
    		{
    			in[i]--;
    			if (in[i] == 0)
    				qu.push(i);
    		}
    	
    	}
    	if (cnt == n)
    		cout << 1;
    	else
    		cout << 0;
    }
    
    int main()
    {
    	int n;
    	cin >> n;
    	
    	for (int i = 0; i < n; i++)
    	{
    		int temp;
    		cin >> temp;
    		for(int j=0;j<temp;j++)
    		{
    			int  temp2;
    			cin>> temp2;
    			egdes[temp][temp2] = 1;
    			in[temp2]++;
    		}
    	}
    	process(n);
    
    }
  • 相关阅读:
    centos PIL 安装
    apache virtualhost 针对ip开放访问
    基础练习 矩形面积交 (分类讨论)
    UVa 10163 Storage Keepers (二分 + DP)
    UVaLive 5009 Error Curves (三分)
    UVa 11542 Square (高斯消元)
    UVa 10828 Back to Kernighan-Ritchie (数学期望 + 高斯消元)
    基础练习 回形取数 (循环 + Java 输入输出外挂)
    UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
    勇敢的妞妞 (状压 + 搜索)
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12789004.html
Copyright © 2020-2023  润新知