可做模板_拓扑排序

/*author: Vit;
 *from: http://blog.csdn.net/svitter
 *if you like it , please show the origin site*/

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

struct Arc
{
    int point;
    int next;
};

Arc arc[50005];
int node[5005];
int digree[5005]; // 入度
int top[5005];

void Topsort()
{
    int n, m;
    scanf("%d%d", &n, &m);//edge = m, vertice = n;
    queue<int>q;
    for(int i = 1; i <= m; i++)
    {
        int a,b;
        scanf("%d%d", &a, &b);//

        arc[i].next = node[a];  //next_arc
        arc[i].point = b;       //权值
        node[a] = i;            //连接点
        digree[b]++;            //入度++
    }

    for(int i = 1; i <= n; i++)
    {
        if(digree[i] == 0)
            q.push(i);
    }

    int l = 0;
    while(!q.empty())
    {
        int x = q.front();
        top[l++] = x;
        q.pop();
        for(int e = node[x]; e != -1; e = arc[e].next)
        {
            digree[arc[e].point]--;
            if(!digree[arc[e].point] == 0)
                q.push(arc[e].point);
        }
    }
}

int main()
{
    Topsort();
    return 0;
}

测试数据为

0 1

0 2

0 3


//============================================================================
// Name        : 拓扑排序.cpp
// Author      : vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

#define MAXV 50

/*
 *ANode为链接上的节点,VNode为原本的节点
 *count为入度,adjvex为数组中对应的点
 */
typedef struct ANode{

	//InfoType info; 节点信息
	int adjvex;//节点值
	struct ANode *nextarc;
}ArcNode;


typedef struct{
	//Vertex data; 顶点信息
	int value;
	int count;	//入度
	ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAXV];

typedef struct{
	AdjList adjlist;
	int n, e;//n为点数,e为边数
}ALGraph;

void TopSort(ALGraph * G){
	int i, j;
	int St[MAXV];
	int top = -1;//栈
	ArcNode *p;

	//入度置初值
	for(i = 0; i <= G->n; i++)
		G->adjlist[i].count = 0;
	//入度赋值

	for(i = 0; i <= G->n; i++){
		p = G->adjlist[i].firstarc;
		while(p != NULL){
			G->adjlist[p->adjvex].count++;
			p = p -> nextarc;
		}
	}
	//如果入度为0, 入栈
	for(i = 0;  i<= G -> n; i ++){
		if(G->adjlist[i].count == 0){
			top ++;
			St[top] = i;
		}
	}

	for(i = 0; i <= G -> n; i++){
		printf("%d:%d\n", i, G->adjlist[i].count);
	}
	printf("\n");

	while(top >-1){
		i = St[top], top--;
		printf("%d", i);
		p = G -> adjlist[i].firstarc;
		//使所有入度相关的点-1
		while(p != NULL){
			j = p -> adjvex;
			G -> adjlist[j].count--;
			if( G-> adjlist[j].count == 0){//入度为零,入栈
				top++;
				St[top] = j;
			}
			p = p -> nextarc;//循环
		}
	}
}

int visited[50];
void DFS(ALGraph * G, int v){//深度优先遍历,递归查找
	ArcNode * p;
	visited[v] = 1;
	printf("%d",v);
	p = G -> adjlist[v].firstarc;
	while(p != NULL){
		if(visited[p->adjvex] == 0)
			DFS(G, p -> adjvex);
		p = p -> nextarc;
	}
}

int main() {
	int i, m, n;
	int temp;
	ANode * t, * t2;
	ALGraph * G;
	memset(visited, 0, sizeof(visited));
	while (cin >> n >> m){
		G = new ALGraph();
		G->n = n;
		for(i = 1;  i <= n; i++){
			scanf("%d %d", &temp, &G->adjlist[i].value);
			if(G->adjlist[temp].firstarc == NULL){

				t = new ANode();
				G->adjlist[temp].firstarc = t;

				t->adjvex = i;
				t->nextarc = NULL;
			}
			else{
				t = G->adjlist[temp].firstarc;

				while(t->nextarc != NULL)
					t = t -> nextarc;

				t2 = new ANode();
				t -> nextarc = t2;
				t2 -> adjvex = i;
				t2 -> nextarc = NULL;
			}
		}

		printf("check\n");//查看图情况
		for(i = 0;  i <= n; i++){
			printf("%d",i);
			t = G->adjlist[i].firstarc;
			if(t == NULL){
				printf("\n");
				continue;
			}
			else{
				while(t != NULL){
					printf("->%d", t->adjvex);
					t = t->nextarc;
				}
			}
			printf("\n");
		}

		printf("TopSort:\n");
		TopSort(G);
	}


	return 0;
}

你可能感兴趣的:(编程,c,算法,拓扑,.模板)