hdu 2647 Reward
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2647
Reward
Description
Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
Input
One line with two integers n and m ,stands for the number of works and the number of demands .$(n \leq 10000,m \leq 20000)$
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1
简单的拓扑排序。。
- #include<algorithm>
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<vector>
- #include<queue>
- #include<map>
- using std::cin;
- using std::cout;
- using std::endl;
- using std::find;
- using std::sort;
- using std::map;
- using std::pair;
- using std::queue;
- using std::vector;
- using std::multimap;
- #define pb(e) push_back(e)
- #define sz(c) (int)(c).size()
- #define mp(a, b) make_pair(a, b)
- #define all(c) (c).begin(), (c).end()
- #define iter(c) decltype((c).begin())
- #define cls(arr,val) memset(arr,val,sizeof(arr))
- #define cpresent(c, e) (find(all(c), (e)) != (c).end())
- #define rep(i, n) for (int i = 0; i < (int)(n); i++)
- #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
- const int N = ;
- typedef unsigned long long ull;
- struct Node {
- int vex, val;
- Node(int i = , int j = ) :vex(i), val(j) {}
- };
- struct TopSort {
- vector<int> G[N];
- int topNum, inq[N];
- inline void init(int n) {
- topNum = ;
- cls(inq, );
- rep(i, n) G[i].clear();
- }
- inline void built(int m) {
- int a, b;
- rep(i, m) {
- scanf("%d %d", &a, &b);
- --a, --b;
- inq[a]++;
- G[b].push_back(a);
- }
- }
- inline void bfs(int n) {
- int ans = ;
- queue<Node> q;
- rep(i, n) {
- if (!inq[i]) { q.push(Node(i, )); topNum++; }
- }
- while (!q.empty()) {
- Node t = q.front(); q.pop();
- ans += t.val;
- rep(i, sz(G[t.vex])) {
- if (--inq[G[t.vex][i]] == ) q.push(Node(G[t.vex][i], t.val + )), topNum++;
- }
- }
- printf("%d\n", topNum == n ? ans : -);
- }
- }work;
- int main() {
- #ifdef LOCAL
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w+", stdout);
- #endif
- int n, m;
- while (~scanf("%d %d", &n, &m)) {
- work.init(n);
- work.built(m);
- work.bfs(n);
- }
- return ;
- }
hdu 2647 Reward的更多相关文章
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward(图论-拓扑排序)
Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- HDU 2647 Reward(拓扑排序,vector实现邻接表)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- 学习记录 Eclipse常用快捷键及其演练
Eclipse中10个最有用的快捷键组合 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask ...
- 洛谷P1828 香甜的黄油 Sweet Butter
P1828 香甜的黄油 Sweet Butter 241通过 724提交 题目提供者JOHNKRAM 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 我的SPFA为什么TLE.. 为 ...
- vc不用IDE编译方法
一个EXE是如何形成的 比如一个源程序有以下两个文件. 1.c 1.rc 首先cl.exe 会把源代码编译为1.obj rc.exe会把1.rc编译为1.res link.exe会把1.obj 1.r ...
- 【HTML/XML 11】XML和HTML的混合使用
导读:在前面介绍了很多关于XML和HTML的东西,他们其实各有各的好处,在很多时候都需要结合起来使用.现在已经有XML和HTML结合的产物:XHTML(可扩展超文本标记语言).在本篇博客中,则主要介绍 ...
- extern “C”调用测试与验证-2016.01.06
1 调用情形说明 在上一篇关于extern “c”原理以及用法中,详细的说明了为什么需要extern “c”以及如何使用它解决c与c++混合编程时遇到的问题.接下来,使用示例验证方式验证c与c++函数 ...
- poj2017
一天两个题,凑数用的大水题啊...不解释了..羞愧ing #include <stdio.h> int main(){ int t; int i; ],b[],tot; ){ tot=; ...
- Android IOS WebRTC 音视频开发总结(六五)-- 给韩国电信巨头做咨询
本文主要总结咨询过程中的一些问题,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam 韩国电信巨头sk想了解国内移动互联网rtc现状,所以上周请我过去给他们的相关人员 ...
- ADO访问Access数据库错误解决心得随笔
最近在用ADO访问Access数据库的时候出现了一个奇怪的错误,觉得有必要记录下来,和大家分享一下. 环境 win7 x86系统: VS2012编译器: Office2010: Access2000~ ...
- CentOS学习笔记—软件管理程序RPM、YUM
软件管理程序 Linux的软件安装分为源代码编译安装和打包安装.RPM是一种打包安装方式,是由 Red Hat 这家公司开发出来的,后来实在很好用,因此很多 distributions 就使用这个机制 ...
- Centos6.5(final)安装gcc和g++,python以及导致问题的解决方法
安装gcc:yum install gcc 安装g++:yum install gcc-c++ 安装python: centos默认是2.6的版本, 下载python ,我下载的是2.7.10. 1 ...