HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward
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<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define pb(x) push_back(x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
const int INF =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 10010;
int n,m;
int a,b;
int in[MAXN];
int sum,cnt;
int price[MAXN];
vector<int>Edge[MAXN];
vector<int> ans;
queue<int> q;
void Init(){
for(int i = 1;i <= n;i++){
Edge[i].clear();
}
memset(in,0,sizeof in);
while(!q.empty()) q.pop();
sum = cnt = 0;
}
int main(){
while(read2(n,m)!=EOF){
Init();
for(int i = 0 ; i < m;i++){
read2(a,b);
Edge[b].push_back(a); //反向建图
in[a] ++;
}
for(int i = 1 ;i <= n ;i++){
if(in[i] == 0){
q.push(i);
price[i] = 888;
}
}
while(!q.empty()){
int p = q.front();
sum += price[p];
cnt++;
q.pop();
for(int i = 0; i < Edge[p].size(); i++){
int y = Edge[p][i];
in[y] --;
price[y] = price[p]+1;
if(in[y] == 0){
q.push(y);
}
}
}
if(cnt < n){
printf("-1\n");
}
else{
print(sum);
}
}
}
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 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- HDU2647(拓扑排序+反向建图)
题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,因为尽可能的让老板少付钱,那么a的工资就是b的工资+1.能够确定关系为a>b,依据拓扑排序建边的原则是把"小于" ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- hdoj--4857--逃生(拓扑排序+反向建图)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
随机推荐
- 添加Google搜索
给自己的网站加一个搜索功能很简单,代码如下: 欢迎访问我的博客:http://67566894.iteye.com/ <form action="http://www.google.c ...
- RepRap Prusa i3 平台自動補正
RepRap Prusa i3 平台自動補正 平台校正不但費時,而且經常失敗,時在是很令人洩氣!期盼了好一陣子,Marlin終於將平台自動補正的功能加進來了!!這個功能將原本Z軸的Endstop,改裝 ...
- How to export a model from SolidWorks to Google SketchUp
How to export a model from SolidWorks to Google SketchUp While Google SketchUp is not a professional ...
- 【Linux学习五】文本处理
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.cut:显示切割的行数据f:选择显示的列s:不显示没有分隔符的行d ...
- Lua 判断表是否为空方法
[1]判断表为空的方法 目前为止,Lua语言中判断table表是否为空有三种方式: (1)#table,当table为数组时直接返回table表的长度. (2)当table是字典时,返回table的长 ...
- c# 定义委托和使用委托(事件的使用)
使用委托时要先实例化,和类一样,使用new关键字产生委托的新实例,然后将一个或者多个与委托签名匹配的方法与委托实例关联.随后调用委托时,就会调用所有与委托实例关联的方法. 与委托关联可以是任何类或者结 ...
- 阿里巴巴 Java 代码规范
1. 抽象类命名使用 Abstratc开头. 2. 阿里强制规定不允许任何魔法值(未经定义的常量)直接出现在代码中.魔法值会让代码的可读性大大降低,而且如果同样的数值多次出现时,容易出现不清楚这些数值 ...
- JAVA基本语法测试
一: 1,JAVA的基本运行单位是类 2,类的成员:成员变量,构造方法,普通方法和内部类 3,成员变量种类:字符类型:char 布尔类型:boolean 数值类型:byte, s ...
- Python+OpenCV图像处理(三)—— Numpy数组操作图片
一.改变图片每个像素点每个通道的灰度值 (一) 代码如下: #遍历访问图片每个像素点,并修改相应的RGB import cv2 as cv def access_pixels(image): prin ...
- Centos搭建Seafile个人网盘
1.安装依赖环境 yum install python python-setuptools python-imaging python-ldap python-memcached MySQL-pyth ...