HDU 5965 Gym Class 贪心+toposort
分析:就是给一些拓补关系,然后求最大分数,所以贪心,大的越靠前越好,小的越靠后越好
剩下的就是toposort,当然由于贪心,所以使用优先队列
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int INF=0x3f3f3f3f;
const LL mod=1e9+;
priority_queue<int>q;
struct Edge{
int v,next;
}edge[N];
int head[N],tot;
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int d[N];
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--){
// printf("Case #%d:\n",++cas);
int n,m;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head)),tot=;
memset(d,,sizeof(d));
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);++d[v];
}
while(!q.empty())q.pop();
for(int i=;i<=n;++i)
if(!d[i])q.push(i);
LL ans=;int cur=-;
while(!q.empty()){
int u=q.top();
q.pop();
if(cur==-)cur=u;
else cur=min(cur,u);
ans+=cur;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
--d[v];
if(!d[v])q.push(v);
}
}
printf("%I64d\n",ans);
}
return ;
}
HDU 5965 Gym Class 贪心+toposort的更多相关文章
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- 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(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))
扫雷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...
- hdu 4004 (二分加贪心) 青蛙过河
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
- Saving HDU(hdu2111,贪心)
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
随机推荐
- Lua中cJson的读写
这里采用的是Lua CJson库,是一个高性能的JSON解析器和编码器,其性能比纯Lua库要高10~20倍.并且Lua Json完全支持UTF-8,无需以来其他非Lua/LuaJit相关包. 环境安装 ...
- MongoDB安装,启动,注册为windows系统服务
MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...
- AnimateWindow 阻塞当前线程问题
今天查了蛮多的,虽然不是系统的学习,收获也不小.下面说一下我的解决方法: 问题一:采用 AnimateWindow API实现消息窗体FormMsg的动画出现,但由于该方法会阻塞当前线程,造成其他窗体 ...
- log4j日志输出到web项目指定文件夹
感谢 eric2500 的这篇文章:http://www.cxyclub.cn/n/27860/ 摘要:尝试将log4j的文件日志输出到web工程制定目录,遇到了很多问题,最终在eric2500的指导 ...
- 【高斯消元】BZOJ 1013: [JSOI2008]球形空间产生器sphere
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...
- 忽然发现,if语句没有相应的continue功能
就是剩下部分语句不用执行了,但是又不退出当前函数,只退出当前if块.虽说else可以解决问题,但是这样还是会重复写代码,假如continue语句后面的内容是相同的话.当然可以通过再次加一个if语句解决 ...
- vs2015 打不开了 提示"CSharpPackage",未能正确加载xx包
原文:vs2015 打不开了 提示"CSharpPackage" 最近发现vs2015 在新建项目和加载现有项目的时候会报错 提示 开始我以为是系统的问题导致vs 配置除了问题,重 ...
- 202. Happy Number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...
- SRM588
250: 有n首歌每首歌有duration和tone,连续唱m首歌会消耗每首歌的duration以及相邻两首歌的tone的差的绝对值的和,给个T,问说在T时间内最对能唱多少歌. 将歌按tone排序后发 ...
- 如何解决MySQLAdministrator 启动报错
运行环境:MySQL 5.1.41 win32 ZIP 非安装版MySQL GUI Tools 5.0(版本1.2.17.0) 运行MySQLAdministrator时提示:服务器服务或配置文件不能 ...