拓扑排序

来源:互联网 发布:淘宝上买药可靠吗 编辑:程序博客网 时间:2024/06/05 17:15






#include<iostream>#include <iomanip>#include<string.h>#define MAX_NUM 10010001using namespace std;void findzeropoint(int indegree[101], int zeropoint[102], int &left, int&right, int N);int main(){int N, M;cin >> N >> M;int a[100][100];//邻接矩阵当做存储的数据结构int first, second, value;for (int i = 0; i <= N; i++)//初始化{for (int j = 0; j <=N;j++)a[i][j] = MAX_NUM;}int indegree[101];//入度int zeropoint[102];//0入度栈int left = 0, right = 0;//两端指针int count = 0;//计数器memset(indegree, 0, 101);//初始化for (int i = 0; i < M; i++)//输入边{cin >> first >> second >> value;a[first][second] = value;indegree[second]++;}findzeropoint(indegree, zeropoint, left, right, N);while (left != right){cout << zeropoint[left++] << endl; count++;for (int i = 1; i <= N; i++)//相关点入度减一{if (a[zeropoint[left-1]][i] != MAX_NUM){indegree[i]--;if (indegree[i]==0)zeropoint[right++] = i;}}}if (count < N)cout << "error" << endl;system("pause");return 0;}void findzeropoint(int indegree[101],int zeropoint[102],int &left,int&right,int N){for (int i = 1; i <= N; i++){if (indegree[i]==0)zeropoint[right++] = i;//入栈}}








0 0
原创粉丝点击