题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647

题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... 让你求最小的值,但是中间出现有向环就不行了,输出-1。

拓扑排序队列实现,因为叶子是最小的,所以把边反向就可以求了。

//拓扑队列实现
//就是先把入度为0的先入队,然后每次出队的时候把相邻的点的入度减1,要是入度为0则再入队,不断循环直到队列为空
//时间复杂度为O(N + E)
//这题就是把边反向就好了
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN = 1e4 + ;
typedef pair <int , int> P;
vector <int> G[MAXN];
int du[MAXN];
int main()
{
int n , m , u , v;
while(~scanf("%d %d" , &n , &m)) {
for(int i = ; i <= n ; i++) {
G[i].clear();
du[i] = ;
}
for(int i = ; i < m ; i++) {
scanf("%d %d" , &u , &v);
G[v].push_back(u);
du[u]++;
}
queue <P> que;
while(!que.empty()) {
que.pop();
}
int cont = , res = ;
for(int i = ; i <= n ; i++) {
if(!du[i]) {
que.push(P(i , ));
res += ;
cont++;
}
}
while(!que.empty()) {
P temp = que.front();
que.pop();
for(int i = ; i < G[temp.first].size() ; i++) {
du[G[temp.first][i]]--;
if(!du[G[temp.first][i]]) {
que.push(P(G[temp.first][i] , temp.second + ));
res += temp.second + ;
cont++;
}
}
}
if(cont == n) {
cout << res << endl;
}
else {
cout << - << endl;
}
}
}

HDU 2647 Reward (拓扑排序)的更多相关文章

  1. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  2. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  3. hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  4. hdu 2647 Reward(拓扑排序+反图)

    题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...

  5. HDU 2647 逆向拓扑排序

    令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...

  6. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  7. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  8. HDU 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. hdu 2647 Reward

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...

随机推荐

  1. 带你走进EJB--那些跟EJB容器相关的那些Java概念

    最近在对EJB的相关内容进行总结,在总结的过程中发现对容器的概念并不是很理解,因为EJB本身就是一个容器,但是容器到底是用来做什么的?它跟我们之前所了解的组件,框架,包,类等都有什么关系?接下来主要是 ...

  2. Java关键字static、final使用小结

    static  1. static变量     按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...

  3. bzoj1975

    显然是类似k短路,直接不停增广即可 好久没写A*了,裸的A*可能会TLE 加点剪枝就卡过去了……… type node=record po,next:longint; cost:double; end ...

  4. Lost connection to MySQL server at 'reading initial communication packet' 错误解决

    Lost connection to MySQL server at 'reading initial communication packet' 错误解决 上次解决了这个问题,今天又碰到,突然失忆, ...

  5. Linq 时间对比陷阱坑

    同样的两个datetime 格式的时间     2013年12月2日 17点29分57秒  

  6. Python interview preparing

    Collection & Recommended: 1. CN - 论坛中看到. - EN 英文原文真的真的很好好好T_T,看得让人感动T_T 总结个人感兴趣的问题(以下部分参照上面): 1. ...

  7. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  8. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  9. 顶 企业站常用css横向导航菜单

    <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...

  10. 再一次见证mssql中in 与exist的区别

    见下面代码 /*+' select * from '+@strDBName +'.dbo.m_aic where nodeid not in(select nodeid from @tmpAIC) ' ...