hdu 2647 Reward(拓扑排序+优先队列)
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
every case ,print the least money dandelion 's uncle needs to
distribute .If it's impossible to fulfill all the works' demands ,print
-1.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
vector<int>v[];
int vis[],degree[];
int n,m,x,y,cnt,flag;
struct node
{
int x;
int y;
bool operator<(const node &a) const
{
return a.y<y;
}
}ans,pos;
void toposort()
{
priority_queue<node>q;
for(int i=;i<=n;i++)
{
if(!degree[i])
{
ans.x=i;
ans.y=;
vis[i]=;
q.push(ans);
}
}
while(!q.empty())
{
pos=q.top();
q.pop();
for(int i=;i<v[pos.x].size();i++)
{
degree[v[pos.x][i]]--;
if(!degree[v[pos.x][i]])
{
ans.x=v[pos.x][i];
ans.y=pos.y+;
q.push(ans);
vis[v[pos.x][i]]=pos.y+;
}
}
}
cnt=flag=;
for(int i=;i<=n;i++)
{
if(vis[i]==-) flag=;
else cnt+=+vis[i];
}
printf("%d\n",flag?-:cnt);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
v[i].clear();
memset(vis,-,sizeof(vis));
memset(degree,,sizeof(degree));
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
v[y].push_back(x);
degree[x]++;
}
toposort();
}
return ;
}
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(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- HDU 5638 Toposort 拓扑排序 优先队列
Toposort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 Description There is a directed acycli ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
随机推荐
- Codeforces Round #274 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...
- UVALive 4192/HDU 2959 Close Enough Computations 数学
Close Enough Computations Problem Description The nutritional food label has become ubiquitous. A sa ...
- Android笔记三十三.BroadcastReceiver使用
广播是一种广泛运用在应用程序之间传输信息的机制,而BroadcastReceiver是对发送出来的广播进行过滤接收并响应的一类组件. BroadcastReceiver本质上是一种全局监听器. ...
- frameset宽屏居中写法
在写frameset的时候发现页面较小的时候不能在屏幕上居中,记录一下frameset页面居中的写法 <frameset cols="*,1280,*" frameborde ...
- ICA(独立成分分析)笔记
ICA又称盲源分离(Blind source separation, BSS) 它假设观察到的随机信号x服从模型,其中s为未知源信号,其分量相互独立,A为一未知混合矩阵. ICA的目的是通过且仅通过观 ...
- 路飞学城Python-Day13
[2.常用模块-模块的种类和导入方法] 1.什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长.越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分 ...
- 【BZOJ3730】震波 - 动态点分治
题意: Description 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i]. 不幸的是,这片土地常常发生地震, ...
- js验证输入框
项目开发中,都会有对表单form中的input输入框进行一些限制验证 如: <!DOCTYPE html><html lang="en"> <head ...
- 红黑树(RBTREE)之上-------构造红黑树
该怎么说呢,现在写代码的速度还是很快的,很高兴,o(^▽^)o. 光棍节到了,早上没忍住,手贱了一般,看到*D的优惠,买了个机械键盘,晚上就到了,敲着还是很舒服的,和老婆炫耀了一把哈哈. 光棍节再去* ...
- 管理windows自启动程序
1. 点击开始,在运行程序框中输入msconfig,然后回车. 在弹出的对话框中,点击”启动“选项卡,在启动项目列表中,把不需要的启动项目前面的对号去掉. 然后切换到”服务“选项卡,这里的服务项目列表 ...