2017-09-12 19:50:58

writer:pprp

最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序,

用来解决有优先级别的排序问题,比如课程先修后修,排名等。

主要实现:用矩阵来储存图,用indegree数组记录每个顶点的入度,

从入度为0的开始,每次删除该入度为0的点,然后修改其他顶点的入度,

在进行查找入度为0的顶点,循环下去就可以

题意如下:给你n个队伍,m个优先顺序,让你输出总的排名的优先顺序。

代码如下:

/*
@theme:拓扑排序 hdu 1285
@writer:pprp
@declare: accepted
@begin:19:00
@end:19:47
@error:
*/ #include <iostream>
#include <cstring>
#include <cstdio> using namespace std;
const int maxn = ;
bool G[maxn][maxn];
int ans[maxn];
int indegree[maxn];//存取入度
int n, m; void init()
{
memset(indegree,,sizeof(indegree));
memset(ans,,sizeof(ans));
memset(G,,sizeof(G));
} void toposort()
{
for(int i = ; i <= n ; i++)
{
for(int j = ;j <= n ;j++)
{
if(G[i][j])
indegree[j]++;
}
}
//应该从入度为0的开始
for(int i = ; i <= n;i++)
{
int k = ;
while(indegree[k]!=)
k++;
ans[i] = k;
//删除根节点
indegree[k] = -;
for(int j = ; j <= n; j++)
{
if(G[k][j])
indegree[j]--;
}
} } int main()
{
//freopen("in.txt","r",stdin);
int x, y;
while(cin >> n >> m)
{
init();
for(int i = ; i < m ;i++)
{
cin >> x >> y;
G[x][y] = ;
} toposort(); for(int i = ; i < n; i++)
cout << ans[i] << " ";
cout << ans[n] << endl;
} return ;
}

分析当前的:

每次删掉一个点以后就要遍历全部的点找到入度为0的点,其实入度为0的点只可能出现在改动之后的点,

所以采用了优先队列,记录下来当前的入度为0的点,将其输出或者储存下来以后,改变由该点指向的点的入度

从这些改动的点中看看有没有入度为0的点,如果有的话,那就将其入队列,直到队列为空,或者记录的已经被删除的点的个数是所有的点

那么退出完成程序

优化代码如下:

/*
@theme:拓扑排序 hdu 1285
@writer:pprp
@declare: accepted
@begin:19:59
@end:20:38
@error:注意保持头脑清醒
*/ #include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio> using namespace std;
const int maxn = ;
int indegree[maxn];
int n, m, x, y; bool G[maxn][maxn]; void topsort()
{
priority_queue<int,vector<int>,greater<int> > pq; for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(G[i][j])
indegree[j]++; for(int i = ; i <= n; i++)
if(indegree[i] == )
pq.push(i),indegree[i] = -1; int counter = ; while(!pq.empty())
{
int v = pq.top();
pq.pop(); if(counter != n)
{
cout << v << " ";
counter++;
}
else
{
cout << v << endl;
break;
} for(int i = ; i <= n;i++)
{
if(G[v][i] == )
{
indegree[i]--;
if(indegree[i] == )
pq.push(i),indegree[i] = -1;
}
}
}
if(counter != n)
cerr << "graph has a cycle." << endl;
} void init()
{
memset(indegree,,sizeof(indegree));
memset(G,,sizeof(G));
} int main()
{
freopen("in.txt","r",stdin);
while(cin >> n >> m)
{
init();
for(int i = ; i < m ; i++)
{
cin >> x >> y;
G[x][y] = ;
}
topsort();
} return ;
}

拓扑排序 - hdu 1285(普通和优先队列优化)的更多相关文章

  1. 拓扑排序 HDU - 5695

    众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之 ...

  2. 拓扑排序 --- hdu 4948 : Kingdom

    Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. hdu1532 用BFS求拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目给出一些点对之间的先后顺序,要求给出一个字典序最小的拓扑排列.对于拓扑排序的问题,我们有DF ...

  4. bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...

  5. hdu 1285 确定比赛名次 拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...

  6. 图论之初,拓扑排序、前向星(通过存储边来存储图)加优先队列对拓扑的优化-----hdu1285

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  8. HDU 5638 拓扑排序+优先队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...

  9. 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

随机推荐

  1. Storm-源码分析- metric

    首先定义一系列metric相关的interface, IMetric, IReducer, ICombiner (backtype.storm.metric.api) 在task中, 创建一系列bui ...

  2. Zipline Risk and Performance Metrics

    Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...

  3. 汉诺塔IV---hdu2077

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077 #include <stdio.h> #include <stdlib.h&g ...

  4. centos下apache安装

    ./configure --prefix=/usr/local/apache2 --enable-so --enable-proxy --enable-proxy-connect --enable-p ...

  5. Swift 语言附注 类型

    本页包括内容: 类型注解(Type Annotation) 类型标识符(Type Identifier) 元组类型(Tuple Type) 函数类型(Function Type) 数组类型(Array ...

  6. 出现 Request Entity Too Large问题的解决方法

    根据经验判断应该是上传文件大小被限制了,检查了应用配置是10M,把它设置成100M,重启服务也不能解决问题. 原来我们的tomcat是通过nginx发现服务代理的,问题就出现nginx服务器上,原来n ...

  7. 简单的SQL注入学习

    引贴: http://blog.163.com/lucia_gagaga/blog/static/26476801920168184648754/ 首先需要编写一个php页面,讲php页面放入/opt ...

  8. sdut3140 A*B(math)

    题目:传送门 题目描述 Your task is to find the minimal positive integer number Q so that the product of digits ...

  9. 素数判断 - C语言实现

    除了1和自身之外不能整除其它数, 称之为素数. 最小的素数是2. 没有最大的素数. 1000以内素数, 如下图所示: 关于素数的算法, 一般有2种. 第1种, 给出一个数n(n >= 2), 判 ...

  10. python subplot 合并子图的方法