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 ...
随机推荐
- [SQL SERVER系列]之常用函数和开窗函数介绍及实例
本文主要介绍SQL SERVER数据库中一些常用的系统函数及其SQL SERVER 2005以上支持的开窗函数. 1.常用函数 --从字符串右边截取指定字符数 select RIGHT('HELLO' ...
- 实现WebApp直接调起NativeApp
原文http://www.baidufe.com/item/3444ee051f8edb361d12.html 试了一个小Demo,从WebApp上直接调起Android Native App,包括应 ...
- 浅谈JavaSccript函数与对象
函数 解剖函数 function One(leve1 , leve2){ //code return leve1+leve2 } 注释: 形参不需要加上类型: return语句为可选,没有return ...
- c++ 字符串工具类
#include <string> #include "util.h" namespace strtool{ std::string trim(const std::s ...
- .substr()在字符串每个字母前面加上一个1
var str = "abcdefghijklmnopq", name = "1", ary = []; for(var i = 0,len = str.len ...
- 破解之寻找OEP[手动脱壳](2)
1.使用ESP定律 OD载入后,F8一次,在寄存器窗口的ESP的内容上(如0012FFA4)右键:“在数据窗口中跟随”,到内存数据窗口,将内存数据窗口以HEX 数据形式显示,在刚才的地址起始位置上(如 ...
- ie6 iframe src="javascript:" 报安全警报问题
<iframe id="shuaka_iframe" class="embed-page-iframe" data-src="https://w ...
- Linux下去掉^M的方法
cat -A filename 就可以看到windows下的断元字符 ^M 要去除他,最简单用下面的命令: dos2unix filename 第二种方法: sed -i 's/^M//g ...
- 1046-第K回文数
描述 回文数是这样一个正整数:它从左往右读和从右往左读是一样的.例如1,111,121,505都是回文数.将1到100,000,000内所有回文数按从小到达排序后,第k个回文数是多少呢? 输入 第一行 ...
- highcharts 多数据+切换
var highchartsOptions = { chart:{ renderTo:'container' }, title:{ text:'指标数据' }, tooltip:{ pointForm ...