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. python - 安装/解释器/变量

    python的官网: https://www.python.org/ Python环境安装 Windows 安装https://www.python.org/downloads/windows/ Wi ...

  2. LeetCode之Symmetric Tree

    </pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ce ...

  3. CListCtrl控件使用方法总结

    今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtr ...

  4. django模型:为已存在的表建立模型

    为已经存在的表建立模型:参考https://blog.csdn.net/opera95/article/details/78200024 为已经存在的表建立模型1.python manage.py i ...

  5. odoo继承父类中的函数(方法)

    使用_inherit继承父类重新设计新类时,可以调用父类中的函数,具体为: 第一步:获得某个模型('model.name')的数据集并进行某种集合操作(model_function),从而获得想要的数 ...

  6. Spring基本功能-扫描与继承

    一.Spring的扫描 一个稍大的项目中,可能会有成百上千个bean,此时采用xml的配置形式注入bean,一方面是配置文件显得十分庞大,另一方面也会导致后期的维护难度增加,为 此,Spring引入了 ...

  7. APP移动端自动化测试工具选型“兵器谱”一览(主流开源工具)

    (下面大多数工具都是开源工具,在github,码云等开源平台都能找到) "测试那点事儿”在看到360旗下的测试团队整理的关于目前APP移动端自动化相关的工具,觉得总结的很到位,对目前大多数中 ...

  8. Java游戏服务器成长之路——感悟篇

    又是一个美好的周末啊,现在一到周末,早上就起得晚,下午困了又会睡一两个小时,上班的时候,早上起来喝一杯咖啡,然后就能高效的工作一整天,然而到了周末人就懒散了,哈哈. 最近刚跳槽,到新公司已经干了有两周 ...

  9. 【Android】ListView、RecyclerView异步加载图片引起错位问题

    今天在RecyclerView列表里遇到一个情况,它包含300条数据,每项包含一个图片,发现在首次载入时,由于本地没图,请求网络的时候:快速滑动导致了图片错位.闪烁的问题. 原理的话有一篇已经说的很清 ...

  10. 【转】js获取当前日期时间“yyyy-MM-dd HH:MM:SS”

    获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function getNowFormatD ...