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 ...
随机推荐
- jqGrid多级表格的实现
原博主链接:http://blog.csdn.net/dreamstar613/article/details/54616503 jqGrid多级表格(可N级) 主要用的方法: subGridRowE ...
- <%=%>、<%%>、<%@%>、<%#%>的区别
1.<%= %> 里面放变量名,获取后台的变量值,直接输入变量到页面上,里面放的变量名,未经过encode eg: 后台: seession["ab"]=ab; 前台: ...
- python下py2exe打包笔记
1.下载与python版本一致的py2exe插件包 2.安装py2exe,安装后在python目录下存在:\Lib\site-packages\py2exe\... 3.新建一个python脚本文件, ...
- 【学习】JMS通信模式
1.关于JMS的点对点模式 JMS的点对点模式下,多个消费者可以注册到同一个队列上,但是生产者的某个消息只能被一个消费者接收,在多个消费者间,生产者的消息被多个消费者循环接收,如当前有6个消息在队列中 ...
- shell-3.bash的基本功能:输入输出重定向
1. 2. 3. 4.
- 常见Json字符串反序列化处理方式总结
常用来处理Json字符串序列化 反序列化组件:Newtonsoft.Json (https://www.newtonsoft.com/json) 参考资料https://www.cnblogs.com ...
- Springboot错误问题总结
进行springboot+swagger2测试的时候,启动项目发现出现这个问题 把所有的类,配置类都注释掉,不管用,百度搜索之后发现一个解决办法, 半信半疑的加到启动类SpringBootApplic ...
- 阿里云部署java项目
第一步:注册阿里云账号(如果有请看第二步) 1.百度搜索阿里云,点击进入阿里云官网 2.点击右上角免费注册 3.进入注册页面,按照要求填写信息 4.注册完成后登陆 登陆之后首先购买阿里云esc与服务器 ...
- poj1284 && caioj 1159 欧拉函数1:原根
这道题不知道这个定理很难做出来. 除非暴力找规律. 我原本找的时候出了问题 暴力找出的从13及以上的答案就有问题了 因为13的12次方会溢出 那么该怎么做? 快速幂派上用场. 把前几个素数的答案找出来 ...
- GenIcam标准(一)
1.概述 如今的数码摄相机包含了很多的功能,而不仅仅是采集图像.对于机器视觉相机来说,处理图像并把结果附加到图像数据流上,控制附加的硬件,代替应用程序作实时的处理等都是很平常的事情.这也导致了相机的编 ...