hdu1285确定比赛名次<拓扑排序>
2012-08-01 11:29
197 查看
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1285
拓扑排序是对有向无环图的一种排序。表示了顶点按边的方向出现的先后顺序。如果有环,则无法表示两个顶点的先后顺序。
拓扑排序方法如下:
(1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它.
(2)从网中删去该顶点,并且删去从该顶点发出的全部有向边.
(3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.
View Code
拓扑排序是对有向无环图的一种排序。表示了顶点按边的方向出现的先后顺序。如果有环,则无法表示两个顶点的先后顺序。
拓扑排序方法如下:
(1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它.
(2)从网中删去该顶点,并且删去从该顶点发出的全部有向边.
(3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.
View Code
#include <stdio.h> #include <string.h> int map[501][501], v[501], in[501], M, N; void Init( ) { memset( map, 0, sizeof map ); memset( in, 0, sizeof in ); memset( v, 0, sizeof v ); int a, b; for(int i=0; i<M; ++ i ){ scanf("%d%d", &a, &b); if(!map[a][b]){ //判断是否重边 map[a][b]=1; in[b]++; //入度 +1 } } } void Topsort(int top ) { int i,flag; while( top<N ){ for( i=1; i<=N; ++ i){ if( !v[i]&&in[i]==0 ) break; } if( i>N && top<N ){ flag=1; //成环 本题没有成环 break; } top++; v[i]=1; printf( top==N?"%d\n":"%d ", i ); for( int j=1; j<=N; ++ j ){ //与i相连的j入度 -1; if( map[i][j]&& in[j] ) in[j]--; } } } int main( ) { while(scanf( "%d%d", &N, &M ) ==2 ){ Init( ); Topsort(0); } return 0; }
相关文章推荐
- HDU 1285 - 确定比赛名次 <拓扑排序>
- hdu 1285 确定比赛名次 <拓扑排序>
- HDU 1285 - 确定比赛名次 <拓扑排序>
- HDU1285 确定比赛名次(拓扑排序)
- hdu1285 确定比赛名次 (拓扑排序)
- 确定比赛名次(HDU1285)(二维数组)
- hdu1285——确定比赛名次
- [hdu1285]确定比赛名次(拓扑排序)
- hdu1285 确定比赛名次(拓扑排序多种方法)
- HDU1285 确定比赛名次
- hdu1285 确定比赛名次【拓扑排序】
- hdu1285确定比赛名次
- HDU1285确定比赛名次(拓扑排序)
- HDU1285-确定比赛名次
- 确定比赛名次---hdu1285(拓扑排序)
- HDU1285 确定比赛名次(拓扑排序)
- hdu1285:确定比赛名次(拓扑排序+优先级队列)
- hdu1285确定比赛名次
- hdu1285 确定比赛名次 (拓扑排序)
- hdu1285 确定比赛名次 拓扑排序