Uva 10305 - Ordering Tasks

本文解析了一道关于任务依赖关系的拓扑排序问题,通过记录每个任务的入度数,采用迭代方式寻找并移除入度为0的任务,直至所有任务完成排序。此方法适用于解决任务执行顺序的问题。

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.

Input

The input will consist of several instances of the problem. Each instance begins with a line containing
two integers, 1 ≤ n ≤ 100 and m. n is the number of tasks (numbered from 1 to n) and m is the
number of direct precedence relations between tasks. After this, there will be m lines with two integers
i and j, representing the fact that task i must be executed before task j.
An instance with n = m = 0 will finish the input.

Output

For each instance, print a line with n integers representing the tasks in a possible order of execution.

Sample Input

5 4
1 2
2 3
1 3
1 5
0 0

Sample Output

1 4 2 5 3

拓扑排序的模板题。

解题思路:

  1. 读入图结构,记录每个节点的入度数(也就是有多少条边的终点为该节点)。
  2. 每次寻找入度为0的点,push_back到ans,然后寻找到的节点的入度减1(入度为-1则后续不会再被考虑),然后将该点连接的点的入度减1。这一步的目的可以理解为将入度为0的点从图中移除,将问题变为求剩下的结点的拓扑排序。
  3. 循环第二步直到所有结点均已进入ans

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 505;
int G[maxn][maxn];
int indegree[maxn];
int n,m,u,v;

int main(int argc, char const *argv[])
{
	while(cin >> n >> m && n != 0)
	{
		memset(indegree,0,sizeof(indegree));
		memset(G,0,sizeof(G));
		
		for(int i = 0;i < m; i++)
		{
			cin >> u >> v;
			G[u][v] = 1;
			indegree[v]++;
		}
		
		vector<int> ans;
		while(ans.size() != n)
		{
			for(int i = 1;i <= n; i++)
			{
				if(indegree[i] == 0)           
				{
					indegree[i]--;              // 将入度变为-1就不再有机会被加入ans 
					ans.push_back(i);
					for(int j = 1;j <= n; j++)   //将i连接的点的入度都减1 
						if(G[i][j])
							indegree[j]--;
				}
			}	
		}
		for(int i = 0;i < ans.size(); i++)
			cout << ans[i] << " ";
		cout << endl;
	}
	return 0;
}


数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
### Pytest-ordering 插件概述 Pytest-ordering 是一个用于控制测试用例执行顺序的插件。该插件允许开发者通过简单的装饰器定义不同测试之间的相对顺序。 ### 安装方法 为了安装 pytest-ordering 插件,在终端中可以使用 `pip` 工具并运行如下命令: ```bash pip install pytest-ordering ``` 对于另一个类似的库 pytest-order,也可以采用相似的方式进行安装[^2]。 ### 使用教程 #### 控制测试用例执行顺序的方法 pytest-ordering 提供了几种不同的方式来设置测试用例的执行顺序。最常见的是利用带有参数的 `@pytest.mark.run()` 装饰器,其中 `order` 参数决定了具体的排列位置;数值越低表示优先级越高,意味着更早被执行。例如: ```python import pytest @pytest.mark.run(order=1) def test_first(): assert True, "This should run first" @pytest.mark.run(order=2) def test_second(): assert True, "This will follow the previous one" ``` 除了显式的数字排序外,还存在一些特殊的标记用来简化某些特定情况下的操作,比如让某个测试总是位于队列开头或结尾。不过需要注意的是,部分早期版本中存在的标签如 `first` 和 `last` 在较新的发行版里已被移除不再推荐使用[^4]。 #### 示例代码展示如何应用这些特性 下面给出了一组完整的例子说明怎样运用上述提到的功能点: ```python import pytest # 设置此测试最先执行 @pytest.mark.run(order=-2) def test_negative_order_example(): print("\nTest with negative order value.") pass # 正常按序号指定先后关系 @pytest.mark.run(order=1) def test_positive_order_one(): print("\nFirst ordered positive number.") pass @pytest.mark.run(order=3) def test_larger_than_default(): print("\nLargest defined position.") pass # 默认情况下未指明次序则放在最后处理 def test_without_explicit_order(): print("\nNo explicit ordering applied here.") pass # 小于零会被安排到非常靠前的位置 @pytest.mark.run(order=-1) def test_most_prioritized(): print("\nMost prioritized due to smallest integer used as its parameter.") pass ``` 当这段脚本被 pytest 执行时,输出会按照设定好的序列依次打印出来,从而验证了各个测试案例确实依照预期进行了编排。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值