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
题目大意:老板发工资,基本工资是888,要求前面的人要比后面的人多,问最少能发多少。如果满足不了要求输出-1
思路:拓扑排序,,先判断有没有环,有环 的话输出-1,一开始我想着前面人的钱比后面人的多,和后面的人比前面的人多结果应该相同。。但是。。wa了N次。
这个题目有两个坑:首相就是 2个人的工资可能相等,第二个就是必须到着来。因为第一个人的工资为基础工资,,也就是说可能会多个人是基础工资。。所以工资必须降序
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1E5+;
vector<int >ve[N];
int re[N];
int in[N];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(re,,sizeof(re));
memset(in,,sizeof(in));
queue<int >que; for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
in[x]++;
ve[y].push_back(x);
} for(int i=;i<=n;i++){
if(in[i]==){
que.push(i);
}
} int n1=;
ll sum=;
while(que.size()){
int xx=que.front();
que.pop();
sum+=+re[xx];
n1++;
for(int i=;i<ve[xx].size();i++){
in[ve[xx][i]]--;
if(in[ve[xx][i]]==){
re[ve[xx][i]]=re[xx]+;
que.push(ve[xx][i]);
}
}
}
if(n1!=n){
puts("-1");
}
else {
printf("%lld\n",sum);
}
for(int i=;i<=n;i++){
ve[i].clear();
}
}
return ;
}
 
 

Reward 杭电 2647的更多相关文章

  1. 杭电 2647 Reward (拓扑排序反着排)

    Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...

  2. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  3. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  4. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  7. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  8. 杭电ACM2076--夹角有多大(题目已修改,注意读题)

    杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...

  9. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

随机推荐

  1. SSI服务器端包含注入

    服务器端嵌入:Server Side Include,是一种类似于ASP的基于服务器的网页制作技术.大多数(尤其是基于Unix平台)的WEB服务器如Netscape Enterprise Server ...

  2. 题解 P5663 【加工零件【民间数据】】

    博客园体验更佳 讲讲我的做法 确定做法 首先,看到这道题,我直接想到的是递归,于是复杂度就上天了,考虑最短路. 如何用最短路 首先,看一张图 我们该如何解决问题? 问题:\(3\)做\(5\)阶段的零 ...

  3. 凸包问题 Graham Scan

    2020-01-09 15:14:21 凸包问题是计算几何的核心问题,并且凸包问题的研究已经持续了好多年,这中间涌现出了一大批优秀的算法. 凸包问题的最优解法是Graham Scan算法,该算法可以保 ...

  4. BIT-逆序数

    2019-12-17 09:42:44 问题描述: 问题求解: 逆序数问题非常经典,使用树状数组可以高效的解决这个问题. public List<Integer> countSmaller ...

  5. 终极解决方案——sbt配置阿里镜像源,解决sbt下载慢,dump project structure from sbt耗时问题

    #sbt下载慢的问题 默认情况下,sbt使用mvn2仓库下载依赖,如下载scalatest时,idea的sbtshell 显示如下url https://repo1.maven.org/maven2/ ...

  6. MySQL优化之执行计划

    前言 研究SQL性能问题,其实本质就是优化索引,而优化索引,一个非常重要的工具就是执行计划(explain),它可以模拟SQL优化器执行SQL语句,从而让开发人员知道自己编写的SQL的运行情况. 执行 ...

  7. 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了.md

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  8. 130ftp-python3 FTP简单实现文件下载(含中文乱码问题)

    130ftp-python3 FTP简单实现文件下载(含中文乱码问题) python3 FTP简单实现文件下载(含中文乱码问题) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. 如何利用python实现为每行添加行数编号

    可能还有更好的方法,在这里我是这么写的,针对小文件可以,但是如果文件内容太多,这种方法感觉不太好 先把所有的数据读取出来,然后利用W覆盖写入模式打开文件进行写入 遍历枚举类型数据后,默认是从0开始,然 ...

  10. mybatis采坑之Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '?&#176;?&#233;?“&#233;?“' in 'field list'

    最后找到问题: 符号问题 修改之后正常