HDU - 5695 Gym Class (优先队列+拓扑排序)
题意:有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 (优先队列+拓扑排序)的更多相关文章
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
- 题解报告:hdu 5695 Gym Class(拓扑排序)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动.今天,它终于当上了梦寐以求的体育课老 ...
- 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu 1285 确定比赛名次 拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Percona-Toolkit 之 pt-kill 用法
生产环境中我们时常遇到这样的情况,数据库性能恶劣,需要马上杀掉部分会话,不然数据库就夯死.我们可以先找show processlist的输出来杀会话,但是比较麻烦.pt-kill为我们解决了杀会话问题 ...
- 「CF521D」Shop
传送门 Luogu 解题思路 当只有第三类操作时,我们显然先进行val较大的操作,这是显然的. 那么就考虑把所有的操作都转变为第三类操作. 第一类操作,显然很容易变为第二类操作:单点维护最大的最终结果 ...
- 如何使用Docker部署PHP开发环境
本文主要介绍了如何使用Docker构建PHP的开发环境,文中作者也探讨了构建基于Docker的开发环境应该使用单容器还是多容器,各有什么利弊.推荐PHP开发者阅读.希望对大家有所帮助. 环境部署一直是 ...
- 使用总结:java多线程总结 <转载>
转载 https://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html java多线程总结 2011-08-28 20:08 Ro ...
- windows驱动不要签名
BCDEDIT -SET LOADOPTIONS DISABLE_INTEGRITY_CHECKSBCDEDIT -SET TESTSIGNING ON
- k8s解析service地址方式
[root@k8s-master ~]# dig -t A kubernetes.default.svc.cluster.local. @10.96.0.10 ; <<>> D ...
- Docker + Maven + Docker-compose
前言: docker:容器化管理 maven:支持docker-maven的插件,通过 mvn clean -Dmaven.test.skip package dockerfile:build 打包命 ...
- spring boot 2.18
@SpringBootAppliction: 标注在某个类,则是springboot的主配置类,springboot就运行这个类的main方法启动springboot; @SpringBootConf ...
- 055、Java中使用for循环输出乘法口诀表
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- IIS7/8 HTTP Error 500.19 错误 0x80070021 错误代码:0x8007000d
nopCommerce versions 4.20 的安装环境是 dotnet-hosting-2.2.0-win.exe .net core项目iis10上出现 HTTP 错误 500.19,错误代 ...