HDU - 5695 Gym Class 【拓扑排序】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5695
思路
给定一些关系 进行拓扑排序
但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 ID大的排在前面
这个就可以用 优先队列
如果不用优先队列 而是每次从大到小去遍历 是不可行的
因为 可能存在 一个点出队后 然后后面有点入队 这个点的ID比当前已经在队伍中的点的ID要大 那么这个点要比那个点先出队
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7;
vector <int> edge[maxn];
int du[maxn];
int n, m;
ll tot;
void toposort()
{
priority_queue <int> q;
for (int i = 1; i <= n; i++)
if (du[i] == 0)
q.push(i);
int Min = INF;
while (!q.empty())
{
int u = q.top();
q.pop();
Min = min(Min, u);
tot += Min;
int len = edge[u].size();
for (int i = 0; i < len; i++)
if ((--du[edge[u][i]]) == 0)
q.push(edge[u][i]);
}
}
void clear()
{
for (int i = 1; i <= n; i++)
{
edge[i].clear();
du[i] = 0;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%d%d", &n, &m);
clear();
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
du[y]++;
edge[x].pb(y);
}
tot = 0;
toposort();
printf("%lld\n", tot);
}
}
HDU - 5695 Gym Class 【拓扑排序】的更多相关文章
- HDU 5695 Gym Class 拓扑排序
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5695 题解: 求出字典序最大的拓扑序.然后把求好的数列翻转过来就是满足条件的数列,然后模拟求一下va ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU - 5695 Gym Class (优先队列+拓扑排序)
题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.在满足这个前提的情况下,将N个人排 ...
- 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)
Gym Class Accepts: 849 Submissions: 4247 Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 4857 (反向拓扑排序 + 优先队列)
题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...
随机推荐
- document.querySelector()与document.querySelectorAll()
document.querySelector()与document.querySelectorAll() CreationTime--2018年7月1日10点49分 Author:Marydon ...
- 51单片机 | 基于I2C总线的秒表模拟应用
———————————————————————————————————————————— 参考地址: http://blog.csdn.net/junyeer/article/details/4648 ...
- Java-帮助文档的制作
Java-帮助文档的制作 1,public修饰的类才干够用bin/javadoc生成文档 2.java的说明书是通过文档的凝视来完毕的,所以在敲代码的时候.凝视是非常有必要的 使用文档凝视法,才干够生 ...
- Kafka备忘
官网 http://kafka.apache.org/ 多生产者多消费者 多topic和多分区 多消费者组.每组中消息不能重复消费,组间不影响 启动 RunKafka(){ cd $kafka_hom ...
- c多线程学习
http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html http://blog.chinaunix.net ...
- quick-cocos2d-x3.2 scheduler使用注意事项
近期在使用scheduler时发现例如以下问题 调用: local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") fun ...
- PHP接收和发送XML数据(json也通用)
一.接收xml数据, 使用php://input,代码如下: <?php $xmldata=file_get_contents("php://input"); $data=s ...
- atitit.bsh BeanShell 的动态脚本使用java
atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器 ,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中. 亦可嵌入到J ...
- Linux下redis安装与使用 (转)
尊重原创:https://www.cnblogs.com/codersay/p/4301677.html,并更正如下红字 redis官网地址:http://www.redis.io/ 最新版本:2.8 ...
- stage3D基础一-----Stage3D如何工作(转)
在如何使用Stage3D系列中的第一个教程中,你将会学习到有关在Flash Player 11中新引入的ActionScript API,该API允许在Flash中利用硬件加速进行3D渲染.在学习这个 ...