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 ...
随机推荐
- select into in mysql
http://stackoverflow.com/questions/16809393/select-into-in-mysql Use the CREATE TABLE SELECT syntax. ...
- MYSQL5.6/5.7 数据库密码丢失问题处理(需重启)
文章结构图: 一.MYSQL5.6密码丢失 1. 强行停止MYSQL 丢失超级管理用户ROOT的密码是致命的,可以通过--skip-grant-tables参数来跳过权限表. 停止MYSQL,强行杀 ...
- xBIM 基础12 WeXplorer xViewer的导航、相机、剖切、隐藏等操作
系列目录 [已更新最新开发文章,点击查看详细] 本篇将学习xViewer的导航,剪切和隐藏.这应该足以使用户能够通过所有缩放,平移,轨道运行和隐藏可能的障碍物来检查建筑物内部.点击这里可以看到 ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- 解决IE下的li中img多余4px的问题--IE6有的问题
为了对比明显,这里用img标签占位,但是背景用纯黑色 <ul> <li> <img src="" alt="" />< ...
- 你不知道的JavaScript(九)switch语句
在c/c++.java这些强类型的语言中switch语句的表达式和case分支中的条件值都只能是char类型或整数.JS的switch语句有些不同,它可以是JS中的任意一种类型,这一点有些朋友可能并没 ...
- 51nod 1402 最大值 3级算法题 排序后修改限制点 时间复杂度O(m^2)
代码: 题意,第一个数为0,相邻的数相差0或者1,有一些点有限制,不大于给定值,求这组数中可能的最大的那个数. 这题我们看一个例子:第5个数的限制为2 1 2 3 4 5 6 7 8 9 0 1 2 ...
- 洛谷P3834 【模板】可持久化线段树 1 主席树
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 2000000 ...
- 【C++】函数和指针
最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解. From C++ Primer Plus: Chapter 7 Function:C++ Programm ...
- Python 线程高级篇 threading 模块的使用
创建一个tread实例,给他传一个函数 #!/usr/bin/python import threading from time import * loops =[4,2] def loop (nlo ...