HDU 2647:Reward(拓扑排序+队列)
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12918 Accepted Submission(s): 4129
Problem 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<=10000,m<=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
题意
老板给员工发工资,n个员工,m中工资关系,每次输入两个人,要求前面的人的工资比后面高。员工的最低工资是888,老板一共需要支付多少工资?如果支付工资是不能满足所有人的要求,输出-1
思路
反向建图,让工资低的那个人的顶点指向工资高的人。然后拓扑排序判断是否有环,有环输出-1 。没有环就将图分层,每一层工资增加1 ,最后输出所需发的工资。
注意建图时用vector,二维数组会爆内存
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define l long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e4+10;
using namespace std;
int n,m;
vector<int> v[maxn];
int vis[maxn];
int money[maxn];
bool toposort()
{
int ans=0;
queue<int>que;
for(int i=1;i<=n;i++)
{
// 把刚开始入度为0的点放入
if(!vis[i])
que.push(i);
}
while(!que.empty())
{
int res=que.front();
que.pop();
// 每次pop出去的点都是入度为0的点,统计这些点的个数
ans++;
for(int i=0;i<v[res].size();i++)
{
vis[v[res][i]]--;
if(vis[v[res][i]]==0)
{
// 将入度为0的点加入队列
que.push(v[res][i]);
// 更新一下后继点的工资,有点难理解,建议画一下图找几组样例试一下
money[v[res][i]]=max(money[res]+1,money[v[res][i]]);
}
}
}
if(ans==n)
return true;
else
return false;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int x,y;
while(cin>>n>>m)
{
ms(vis);
ms(money);
for(int i=0;i<maxn;i++)
v[i].clear();
for(int i=0;i<m;i++)
{
cin>>x>>y;
// 反向建图
v[y].push_back(x);
vis[x]++;
}
if(!toposort())
cout<<-1<<endl;
else
{
ll ans=888*n;
for(int i=1;i<=n;i++)
ans+=money[i];
cout<<ans<<endl;
}
}
return 0;
}
HDU 2647:Reward(拓扑排序+队列)的更多相关文章
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
随机推荐
- Caffe 学习系列
学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...
- Java-Java面向对象程序设计
2017-10-09 17:23:52 在面向对象技术中,将客观世界中的一个事物作为一个对象来考虑,比如有个张先生,他就是一个对象.每个对象都有自己的属性和行为.张先生的属性根据需要有姓名.性别.身高 ...
- oracle 临时表的使用
在oracle中,临时表分为会话级别(session)和事务级别(transaction)两种. 会话级的临时表在整个会话期间都存在,直到会话结束:事务级别的临时表数据在transaction结束后消 ...
- 3.5 MIPS体系结构
计算机组成 3 指令系统体系结构 3.5 MIPS体系结构 MIPS是精简指令系统的代表,采用了与X86相反的设计理念,并引领了精简指令系统的潮流,那就让我们一起来看一看这究竟是怎么一回事. 要探讨M ...
- Jersey 2.x 探索新建的工程
如果用 Jersey maven archetype 成功创建了这个项目,那么在你当前的路径下就已经创建了一个名为simple-service项目.它包含了一个标准的Maven项目结构: 说明 文件目 ...
- 51 jquery 节点操作和 bootstrapt
jquery 和 bootstrapt1.jquery each 函数 1.each 循环方式一: 可循环对象: var arr =["alex","deng" ...
- ural Ambitious Experiment 树状数组
During several decades, scientists from planet Nibiru are working to create an engine that would all ...
- Thrift0.11.0基于Intellij IDEA的简单的例子
前言 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中所用到的数据传输方式包括 XML,JSON 等,然 ...
- iterator not dereferencable问题
STL中的迭代器总是出现各种问题,这个是我在打表达式求值时碰到的... 综合网上的答案,一般来说有两种情况: 第一:访问了非法位置. 一般来说可能在queue为空时取front(),rear(),或者 ...
- nyoj860(01变形)
http://acm.nyist.net/JudgeOnline/problem.php?pid=860 又见01背包 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 ...