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
#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(拓扑排序+优先队列)的更多相关文章

  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 (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  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 5638 Toposort 拓扑排序 优先队列

    Toposort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 Description There is a directed acycli ...

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

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

  8. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  9. 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

随机推荐

  1. CxImage的使用及基本用法

    基本定义:CxImage类库是一个优秀的图像操作类库.它可以快捷地存取.显示.转换各种图像. 下载地址:http://www.codeproject.com/KB/graphics/cximage.a ...

  2. 指向类成员函数的函数指针及#define typedef 实现类成员函数的类型转换

    #include <iostream> using namespace std; class Test { public : void print() { cout << &q ...

  3. (转载)详解7.0带来的新工具类:DiffUtil

    [Android]详解7.0带来的新工具类:DiffUtil 标签: diffutil 2017-04-17 18:21 226人阅读 评论(0) 收藏 举报  分类: Android学习笔记(94) ...

  4. asp.net HTTP请求过程

    http://blog.csdn.net/zxxSsdsd/article/details/51909860

  5. poj1170 - 转换成背包

    题目链接 有5种物品,给出每个物品的单价. 给出几个这些物品的组合和这个组合的价格.买组合要比一件件的买便宜. 问给定的购买计划最少花多少钱. ---------------------------- ...

  6. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  7. JS 前端 将图片转换为Base64利用H5 FileReader新特性

      file = document.getElementById("image"); var file=file.files[0]; var fileName=file.name; ...

  8. [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂

    Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...

  9. vue.js的<slot>

    使用插槽分发内容在封装vue组件的时候,很多时候就不得不使用到vue的一个内置组件<slot>.slot是插槽的意思,顾名思义,这个<slot>组件的意义是预留一个区域,让其中 ...

  10. Linux内存管理与C存储空间

    ELF文件 在学习之前我们先看看ELF文件. ELF分为三种类型:.o 可重定位文件(relocalble file),可执行文件以及共享库(shared library),三种格式基本上从结构上是一 ...