ZOJ 1060 poj 1094 Sorting It All Out(拓扑排序或 弗洛德)

本文介绍了一种通过拓扑排序来确定元素间关系的方法,并提供了一个具体的实现案例。该案例涉及输入一组元素之间的部分排序关系,然后使用拓扑排序算法来判断这些关系是否能够构成一个完整的排序序列,或者是否存在矛盾之处。

Sorting It All Out

Time Limit: 2 Seconds Memory Limit: 65536 KB

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.


Source: East Central North America 2001
Submit Status


思路:拓扑排序,判断是否出现环,或者能排出序列,当出现多于一个入度为0的点则为不确定。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
const int mm=1100;
char out[mm];
bool vis[77];
vector<int >mp[77];
int cu[mm],cun[mm],n,m,pos;
bool yes;
int topsort()
{ for(int i=0;i<30;i++)
  cu[i]=cun[i];
  int top=-1,ttnum=0;
  for(int i=0;i<n;i++)
    if(cu[i]==0)
    cu[i]=top,top=i;
    if(cu[top]!=-1)yes=1;///只要入度为0的点多于1个那么就是不确定序列,但人需要判断是否有环
  for(int i=0;i<n;i++)
  {
    if(top==-1)return 0;
    else
    {
      int num=top;top=cu[top];
      pos+=sprintf(out+pos,"%c",char('A'+num));
      for(int j=0;j<mp[num].size();j++)
        if(--cu[mp[num][j]]==0)
        cu[mp[num][j]]=top,top=mp[num][j];
        if(top>=0&&cu[top]!=-1)yes=1;
    }
  }return 1;
}
int main()
{
  while(cin>>n>>m)
  { if(n==0&&m==0)break;
   char sss,u,v;
   memset(cun,0,sizeof(cun));
   memset(mp,0,sizeof(mp));
   memset(vis,0,sizeof(vis));
   int ans=0,z=0,pp=0;
    for(int i=0;i<m;i++)
    {
      cin>>u>>sss>>v;
      cun[v-'A']++;mp[u-'A'].push_back(v-'A');
      if(!vis[v-'A'])vis[v-'A']=1,pp++;
      if(!vis[u-'A'])vis[u-'A']=1,pp++;///判断不同的字符数是否刚好为n个
      if(ans==0)
      {pos=0;yes=0;///yes是真说明拓扑排序的序列仍是不确定,但需要看看是否有环,无环则continue;
       if(topsort()){if(yes)continue;if(pp==n)ans=1,z=i+1;}
       else ans=-1,z=i+1;
      }
    }
    if(ans==1)cout<<"Sorted sequence determined after "<<z<<" relations: "<<out<<".\n";
    else if(ans==-1)cout<<"Inconsistency found after "<<z<<" relations.\n";
    else cout<<"Sorted sequence cannot be determined.\n";

  }
}




已经博主授权,源码转载自 https://pan.quark.cn/s/fb533687a163 《C++经典代码大全》是一部专门针对C++入门者的重要参考资料,其核心目标在于提供易于理解的C++编程范例,旨在协助新学者迅速领会C++语言的关键概念与技术要点。此压缩文件所包含的信息许涵盖了从基础到高级的各类C++编程技巧,涉及面向对象编程中的类与对象、函数的应用、程序流程控制、数据结构设计、模板技术以及异常管理等多个关键领域。 1. **基础语法** - 变量声明与初始化:掌握如何声明并初始化不同数据类型的变量,例如整型(int)、浮点型(float)、字符型(char)等。 - 基本输入输出:学习运用`std::cin`和`std::cout`执行标准数据输入与输出操作。 - 控制流语句:熟练运用条件语句(if、if-else、switch-case)以及循环语句(for、while、do-while)来控制程序流程。 2. **类与对象** - 类的定义:学会如何构建类,包含其成员变量与成员函数的设定。 - 对象的创建与使用:掌握如何实例化对象,并经由对象访问类的成员函数。 - 封装:理解封装的理念,并学习使用private和public访问修饰符来保护数据。 - 构造函数与析构函数:掌握如何为类定义自定义的构造过程与析构过程。 3. **函数** - 函数的定义与调用:理解函数的功能与作用,以及如何进行函数的定义和调用。 - 函数参数:精通不同类型的参数传递方法,包括值传递和引用传递。 - 函数重载:学习在同一作用域内定义多个具有相同名称但参数列表不同的函数。 - 函数指针:了解函数指针的运用方法,及其在回调函数和模板中的应用场景。 4. **数组与字符串** -...
内容概要:本文研究了一种计及自适应预测修正的微电网模型预测控制(MPC)优化调度方法,并提供了Matlab代码实现。该方法针对微电网中风电出力等可再生能源的强不确定性,引入自适应预测修正机制,动态调整预测模型以提升短期功率预测精度,从而增强调度决策的准确性与系统运行的鲁棒性。研究构建了完整的MPC滚动优化框架,涵盖预测模型建立、多时间尺度优化求解、实时反馈校正等关键环节,实现了系统运行成本最小化、能源高效利用与功率平衡的多重目标。所提方法有效应对了负荷波动与新能源出力随机性带来的调度挑战,提升了微电网能量管理系统的智能化水平。; 适合人群:具备电力系统、自动化、控制理论相关领域基础知识的研究生、科研人员及工程技术人员,尤其适合从事微电网优化、可再生能源集成、模型预测控制研究的专业人士,熟悉Matlab编程与优化算法者更佳。; 使用场景及目标:①应用于高比例可再生能源接入的微电网能量管理系统,提升调度方案的实时性与鲁棒性;②为不确定性环境下电力系统动态优化控制策略的研究提供仿真验证平台;③支持学术论文复现、科研课题攻关及实际工程项目的前期技术验证与方案预研。; 阅读建议:建议结合Matlab代码逐模块分析算法实现细节,重点关注预测模型构建与反馈修正机制的设计逻辑,通过调整风电出力、负荷需求等场景参数进行仿真实验,深入理解MPC在微电网调度中的滚动优化特性与自适应修正能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值