HDU1285基于有向图邻接表的优先队列的拓扑排序
2011-12-28 15:44
337 查看
#include <iostream> #include <queue> using namespace std; //有向图的邻接表 typedef struct v { int vex;//终点的序号 v *next;//与这条有向边具有相同起点的其它有向边 }V;//边结点 typedef struct h { int indegree;//入度 v *next;//指向((从该节点出发的有向边的)边结点所组成的单链表) }H;//头结点 H team[10010]; V *p; int main() { int i,n,m,a,b,count; priority_queue<int,vector<int>,greater<int> > q; while(cin>>n>>m) { memset(team,0,sizeof(team)); while(m--) { //加入一条a->b的有向边 cin>>a>>b; team[b].indegree++; p=new V; p->vex=b; p->next=team[a].next; team[a].next=p; } //输出拓扑排序 for(i=1;i<=n;i++) if(team[i].indegree==0) q.push(i); count=0; while(!q.empty()) { a=q.top(); q.pop(); if(count) cout<<' '; cout<<a; count++; for(p=team[a].next;p!=0;p=p->next) { b=p->vex; if(--team[b].indegree==0) q.push(b); } } cout<<endl; } return 0; }
相关文章推荐
- [hdu1285]拓扑排序模板 邻接表加优先队列简单易懂
- 图论之初,拓扑排序、前向星(通过存储边来存储图)加优先队列对拓扑的优化-----hdu1285
- 《拓扑排序》hdu acm 5.2.4 c++优先队列 邻接表
- HDU1285 确定比赛名次【拓扑排序】【优先队列】
- Hdu 3342 Legal or not(基于邻接表的BFS+队列的拓扑排序)
- 数据结构实验图论:基于邻接矩阵/邻接表的广度优先搜索遍历
- 拓扑排序(基于邻接表实现)
- 用java实现一个基于堆排序的优先队列
- 基于邻接表储存的图的深度优先和广度优先遍历
- 基于邻接表实现的DFS深度优先搜索
- 算法(第四版)学习笔记之java实现基于堆的优先队列
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 26.基于 有向图的邻接表表示 实现 AOV网 的拓扑排序
- 基于堆的优先队列实现
- 拓扑排序 详解 + 并查集 详解 + 最小生成树(MST)详解 【普利姆算法 + 优先队列优化 & 克鲁斯卡尔算法】
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 有向图(4)--基于深度优先的拓扑排序
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- 算法设计之,堆,堆排序,基于最大堆的最大优先队列的实现(C++实现)
- 排序笔记_7(基于堆得优先队列)