D-OJ刷题日记:使用邻接表实现AOV网的拓扑排序算法 题目编号:1137

使用邻接表实现AOV网的拓扑排序算法 题目编号:1137

  • 题目描述
  • 评论

用邻接表存储有向图,在顶点表中增加入度域,使用队列存储入度为零的顶点编号,实现AOV网的拓扑排序算法,并输出拓扑序列,顶点个数少于20个。

部分提示代码:

#include

using namespace std;

struct Arcnode

{

int adjvex;

Arcnode *next;

};

struct Vertexnode

{

int in;

char vertex;

Arcnode *firstedge;

};

const int Maxsize = 20;

class ALgraph

{

public:

ALgraph(char a[],int n,int e);

~ALgraph();

void TopSort();

private:

Vertexnode adjlist[Maxsize];

int vertexnum;

int arcnum;

};

ALgraph::ALgraph(char a[],int n,int e)

{

  //write your code

}

ALgraph::~ALgraph()

{

int i;

Arcnode *p;

for(i=0;i

{

p = adjlist[i].firstedge;

while(p)

{

Arcnode *q = p;

p = p->next;

delete q;

}

}

}

void ALgraph::TopSort()

{

//write your code

}

int main()

{

int i,n,m;

cin >>n>>m;

char *b = new char[n];

for (i = 0;i < n;i++)

{

cin >>b[i];

}

ALgraph al(b,n,m);

al.TopSort();

return 0;

}

输入描述

首先输入图中顶点个数和边的条数;
输入顶点的信息(字符型);
输入各顶点的入度;
输入各边及其权值。

输出描述

输出AOV网的拓扑序列(顶点信息),以空格隔开,最后一个顶点后面有空格,如果AOV网存在回路,输出"有回路"的信息,占一行。

输入样例

6 9
A B C D E F
3 0 1 3 0 2
1 0
1 3
2 0
2 3
3 0
3 5
4 2
4 3
4 5

输出样例

B E C D F A 

注意:AC:50%,不知到哪里出的问题,有没有过了的大佬指点迷津??

#include 

#include

using namespace std;

struct Arcnode
{
	int adjvex;
	Arcnode* next;
};

struct Vertexnode
{
	int in;//入度
	char vertex;
	Arcnode* firstedge;
};

const int Maxsize = 20;

class ALgraph
{
public:
	ALgraph(char a[], int n, int e);
	~ALgraph();
	void TopSort();//拓扑排序
private:
	Vertexnode adjlist[Maxsize];//头表
	int vertexnum;
	int arcnum;
};

ALgraph::ALgraph(char a[], int n, int e)
{
	vertexnum = n;

	arcnum = e;
	
	for (int i = 0; i < vertexnum; i++)//顶点初始化
	{
		adjlist[i].vertex = a[i];
	
		adjlist[i].firstedge = NULL;
	}

	for (int i = 0; i < vertexnum; i++)//输入各顶点入度
		
		cin >> adjlist[i].in;

	Arcnode* s = NULL, * r = NULL;

	for (int k = 0; k < arcnum; k++)//尾插法
	{

		int i , j ;

		cin >> i >> j;
	
		//r = adjlist[i].firstedge;

		s = new Arcnode;

		s->adjvex = j;

		//if (r == NULL)
		//{
			s->next = adjlist[i].firstedge;

			adjlist[i].firstedge = s;
		//}
		/*else
		{
			s->next = r->next;

			r->next = s;

			r = r->next;
		}*/
	}

}

ALgraph::~ALgraph()
{
	int i;
	Arcnode* p;
	for (i = 0; i < vertexnum; i++)
	{
		p = adjlist[i].firstedge;
		while (p)
		{
			Arcnode* q = p;
			p = p->next;
			delete q;
		}
	}

}

void ALgraph::TopSort()
{
	int i, j, k, count = 0;
	
	queue S;

	Arcnode* p = NULL;

	for (i = 0; i < vertexnum; i++)
	{
		if (adjlist[i].in == 0) S.push(i);
	}

	while (!S.empty())
	{
		j = S.front();

		S.pop();

		cout << adjlist[j].vertex<<' ';

		count++;
	
		p = adjlist[j].firstedge;

		while (p != NULL)
		{
			k = p->adjvex;

			adjlist[k].in--;

			if (adjlist[k].in == 0) S.push(k);

			p = p->next;
		
		}

	}

	if (count < vertexnum) cout << "有回路";

}

int main()
{
	int i, n, m;

	cin >> n >> m;

	char* b = new char[n];

	for (i = 0; i < n; i++)
	{
		cin >> b[i];
	}

	ALgraph al(b, n, m);

	al.TopSort();

	return 0;
}

你可能感兴趣的:(每日一题,算法,图论,数据结构)