题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数。在满足这个前提的情况下,将N个人排成一队,求所有同学评价分数和的最大值。

分析:

1、A不希望B排在他之前,所以B要排在A之后,也就是说A安排好后才能安排B,即A对B是一种限制,显然拓扑排序。

2、因为每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数。因此ID大的同学越靠前,最后能得出的分数和越大,因此优先队列。

3、预处理每个人的入度,如果A对B有限制,则B的入度加1。

4、将入度为0的点加入优先队列,并将该点所限制的点入度-1。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
priority_queue<int> q;
vector<int> v[MAXN];
vector<int> ans;
int indegree[MAXN];
void init(){
while(!q.empty()) q.pop();
for(int i = 0; i < MAXN; ++i) v[i].clear();
ans.clear();
memset(indegree, 0, sizeof indegree);
}
void deal(int x){
int len = v[x].size();
for(int i = 0; i < len; ++i){
--indegree[v[x][i]];
if(!indegree[v[x][i]]) q.push(v[x][i]);
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
init();
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; ++i){
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
++indegree[b];
}
for(int i = 1; i <= n; ++i){
if(!indegree[i]){
q.push(i);
}
}
while(!q.empty()){
int tmp = q.top();
ans.push_back(tmp);
q.pop();
deal(tmp);
}
int len = ans.size();
int _min = INT_INF;
LL tot = 0;
for(int i = 0; i < n; ++i){
_min = min(_min, ans[i]);
tot += LL(_min);
}
printf("%lld\n", tot);
}
return 0;
}

  

HDU - 5695 Gym Class (优先队列+拓扑排序)的更多相关文章

  1. HDU - 5695 Gym Class 【拓扑排序】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...

  2. 题解报告:hdu 5695 Gym Class(拓扑排序)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动.今天,它终于当上了梦寐以求的体育课老 ...

  3. 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  4. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  5. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  6. HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  7. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

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

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

  9. hdu-5695 Gym Class(贪心+拓扑排序)

    题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. english-phoneme

    1. 声音概述 2. 音素phoneme与音标 2.1 音素与音标 2.2 音素与字母 2.3 字母发音-字母自然发音对照表 2.4 音标表 2.5 元音字母-辅音字母表 2.6 单元音发音口形趋势表 ...

  2. express写的接口在疯狂刷新几十次后,服务器挂掉

    用到的命令行: show status like 'Threads%'; show variables like '%max_connections%'; show global status lik ...

  3. leetcode813 Largest Sum of Averages

    """ We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...

  4. C++ Primer Plus 6 笔记(1)

    1-3章 1.cin.get(),在程序末尾,让窗口一直打开. 2.c++版本的math.h为cmath 3.<< 在c++中是插入运算符,在c中是左移运算符 4.&在C中既表示地 ...

  5. POJ 3233:Matrix Power Series 矩阵快速幂 乘积

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 18450   Accepted:  ...

  6. HomePod即将发售,但硬件不再是苹果的救命稻草

    流年不利的苹果,在多个维度都遭到了重创.除了与高通纠缠不清的专利官司外,iPhone销量还直线下滑并影响到营收.最终,苹果股价.市值都处于暴跌态势.面对内外夹击的不利局面,苹果信奉多年的"封 ...

  7. Vuex - state , getters , mutations , actions , modules 的使用

      1, 安装   vue add vuex 2, 安装完之后会自动生成store文件夹,并在main.js中自动引用 store/index.js 3,在store文件夹下的index.js中定义 ...

  8. 基于vue-router的移动端网页的路由管理

    本篇代码示例:github 前提:不关注移动端浏览器的前进事件 涵盖功能: 1,管理路由的历史记录 2,切页动画的实现 3,处理流程类页面的回退事件 描述:    流程类页面的回退事件的解释: 以注册 ...

  9. Java笔记--集合

    1.Java集合类可以用于存储数量不等的多个对象,还可以用于保存具有映射关系的关联数组. 2.Java集合可分为Collection和Map两种体系: --Collection:1)Set:元素无序. ...

  10. 机器学习(ML)八之正向传播、反向传播和计算图,及数值稳定性和模型初始化

    正向传播 正向传播的计算图 通常绘制计算图来可视化运算符和变量在计算中的依赖关系.下图绘制了本节中样例模型正向传播的计算图,其中左下角是输入,右上角是输出.可以看到,图中箭头方向大多是向右和向上,其中 ...