//AOV网与拓扑排序类
#ifndef _AOV_H_
#define _AOV_H_
#include<iostream>
#include<cstdio>
#include<malloc.h>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#endif struct point
{
int vertex;//顶点
point* next;
}; class AOV
{
private:
int* ok;
int* indegree;//入度
point* aim;//邻接表
bool n_hasvalue;
int n; public:
int* ans;
AOV(int maxn,int point_num);//构造函数原型
AOV():AOV(,){};//直接构造函数
AOV(int maxn);//容器型构造参数
void maxpoint(int p);
int readin();
int* topo_sort();
}; void AOV::maxpoint(int p)
{
if(n_hasvalue==true)
{
printf("Sorry n had value,ERROR!\n");
return;
}
n=p;
n_hasvalue=true;
return;
} AOV::AOV(int maxn)//容器型构造参数
{
ans=new int[maxn];
ok=(int*)new int[maxn];
memset(ok,,sizeof(int)*maxn);
//这里的重置不能写成sizeof(ok);
indegree=new int[maxn];
aim=new point[maxn];
n_hasvalue=false;
} AOV::AOV(int maxn,int point_num)
{//maxn描述可能使用到的最大的顶点数量
ans=new int[maxn];
ok=(int*)new int[maxn];
memset(ok,,sizeof(int)*maxn);
indegree=new int[maxn];
aim=new point[maxn];
n_hasvalue=true;
n=point_num;
} int AOV::readin()
{
if(n_hasvalue==false){printf("n do not has value!\n");return ;}
memset(indegree,,sizeof(int)*(n+));
for(int i=;i<n;i++)
{
aim[i].next=NULL;
aim[i].vertex=i;
}
//初始化
int a,b;
//printf("Please input pairs of numbers, which from 0 to n-1, to describe the relationship.\n");
//printf("When there is a pair of numbers consists of two 0,then input will stop.\n");
while(cin>>a>>b)
{//a->b
if(a==&&b==)break;
if(a>=n||b>=n||a<||b<){printf("Data error\n");continue;}
indegree[b]++;//入度加1
point* temp=&aim[a];
while(temp->next!=NULL)temp=temp->next;
//找到存有指向结点链表的末端
temp->next=(point*)malloc(sizeof(point));
temp=temp->next;//进入新的point点
temp->vertex=b;//a->b
temp->next=NULL;
}//完成邻接表的构建
return ;
} int* AOV::topo_sort()
{
// for(int i=0;i<n;i++)
// {
// printf("vertex %d indegree %d points to:",aim[i].vertex,indegree[i]);
// point* temp=&aim[i];
// while(temp->next!=NULL)
// {
// temp=temp->next;
// printf("%d ",temp->vertex);
// }
// printf("\n");
// }
queue<int> psd;
int cur=;
int num=n;
while()
{
if(num)
{
for(int i=;i<n;i++)
{
if(ok[i])continue;
if(indegree[i]==)
{
psd.push(i);
ok[i]=;
num--;
}
}//检查所有入度0的顶点并入队,留下入队标记
}
if(psd.empty())break;//队列为空则排序结束
int p=psd.front();psd.pop();
point* temp=&aim[p];
ans[cur++]=p;//也可以写成ans[cur++]=aim[i].vertex;
//printf("%d ",p);
//提出结点并排序
while(temp->next!=NULL)
{
temp=temp->next;
indegree[temp->vertex]--;
}//去掉相关有向边
}
//printf("\n\ncur->%d\n",cur);
return ans;
}

最新文章

  1. *HDU 1028 母函数
  2. (function(){...}())与(function(){...})()
  3. java中并不是任意多个接口都可以实现多实现
  4. C++: read SQL server data using System::Data::SqlClient
  5. Linux Gitlab
  6. 在Word2013中多次应用格式刷
  7. iOS - CoreMotion
  8. BZOJ2661 连连看 (费用流)
  9. .NET 轻量级 ORM 框架 - Dapper 介绍
  10. android 通过shape设置圆形按钮
  11. mysql数据库的物理文件结构
  12. Troubleshooting OpenStack Bug- 每天5分钟玩转 OpenStack(162)
  13. mysql全日志(general log)的命令名称
  14. ●BZOJ 1969 [Ahoi2005]LANE 航线规划
  15. centos7安装supervisor
  16. 【转载】 github vue 高星项目
  17. UVa514 Rails (栈)
  18. Kudu Native RDD
  19. clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系
  20. oracle闪回数据

热门文章

  1. QT笔记:数据库总结
  2. [PowerShell]Python虚拟环境激活失败
  3. Elasticsearch与中文分词配置
  4. Go语言实现:【剑指offer】不用加减乘除做加法
  5. ArtiPub:一款开源的一文多发平台
  6. php curl 相关知识
  7. VFP CursorAdapter 起步一(作者:Doug Hennig 译者:fbilo)
  8. Webpack中hash、chunkhash和contenthash三者的区别
  9. ES[7.6.x]学习笔记(一)Elasticsearch的安装与启动
  10. Hibernate入门之创建数据库表