Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13509    Accepted Submission(s): 4314

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
 
Author
dandelion
 
Source
 
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  3342 1811 2680 2112 2729 
 
分析:
a b:表示a的钱应该比b多,基本工资是888,所以a最少是889
坑点:
1.必须使用邻接表存图,邻接矩阵会超内存
2.可能存在多条拓扑路径,所以不能最后直接算总工资(有点没有说明白,就是有的人工资可以和其他人一样,看下面的两组数据就知道了)
数据:
5 5
1 2
2 3
4 5
2 3
4 5

答案:4444

5 4
1 2
2 5
2 4
4 3

答案:4446

 
所以我们队列里面得存结构体!!!
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 10005
int indgree[max_v];
struct node
{
int index,v;
node(int x,int y)
{
index=x;
v=y;
}
};
vector <int> vv[max_v];
int n,m;
LL sum;
queue<node> q;
int tpsort()
{
sum=;
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
{
if(indgree[i]==)
q.push(node(i,));
} int c=;
int flag=;
int p1,p2;
while(!q.empty())
{
if(q.size()>)
flag=;
p1=q.front().index;
p2=q.front().v;
q.pop();
c++;
sum+=p2;
for(int i=;i<vv[p1].size();i++)
{
indgree[vv[p1][i]]--;
if(indgree[vv[p1][i]]==)
q.push(node(vv[p1][i],p2+));
}
}
/*
拓扑完之后,存在没有入队的点,那么该点就一定是环上的
*/
if(c!=n)//存在环
return ;
else if(flag)//能拓扑但存在多条路
return ;
return -;//能拓扑且存在唯一拓扑路径
}
int main()
{
int x,y;
while(~scanf("%d %d",&n,&m))
{
memset(indgree,,sizeof(indgree));
for(int i=;i<=n;i++)
{
vv[i].clear();
}
int flag=-;
for(int i=;i<m;i++)
{
scanf("%d %d",&y,&x);
if(count(vv[x].begin(),vv[x].end(),y)==)//防止重边
{
vv[x].push_back(y);
indgree[y]++;
}
if(count(vv[y].begin(),vv[y].end(),x)!=)//环的一种
{
flag=;
}
}
flag=tpsort();
if(flag==)
{
printf("-1\n");
}else
{
printf("%lld\n",sum);
}
}
}

HDU 2647 Reward(拓扑排序,vector实现邻接表)的更多相关文章

  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(拓扑排序+优先队列)

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

  5. hdu 2647 Reward(拓扑排序+反图)

    题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...

  6. HDU 2647 逆向拓扑排序

    令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...

  7. HDU 1285--确定比赛名次【拓扑排序 &amp;&amp; 邻接表实现】

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

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

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

  9. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  10. 利用C++ STL的vector模拟邻接表的代码

    关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...

随机推荐

  1. Codeforces343D(SummerTrainingDay06-F dfs序+线段树)

    D. Water Tree time limit per test:4 seconds memory limit per test:256 megabytes input:standard input ...

  2. CentOS 7环境下Pycharm安装流程记录

    1.准备安装文件: 方法1: 使用内置火狐浏览器访问下载最新格式为tar.gz的压缩包 网址:https://www.jetbrains.com/pycharm/download/previous.h ...

  3. python解释器介绍以及Pycharm的破解

    python语言是弱类型解释型语言,弱类型指的是没有强制规定它的类型. 由于是解释型语言,则必有解释器与其匹配,根据不同的工作环境以及需求,python的解释器有很多种, 官方推荐的是CPython, ...

  4. github 账号创建

    1.注册 注册地址:   https://github.com/join?source=header-home 2.建立组织 (1)点击头像旁边的"+",选择New organiz ...

  5. HTML5 简单归纳 -- 前端知识 (二)

    HTML5 全屏事件 全屏事件:requestFullScreen 关闭全屏:cancelFullScreen 判断是否全屏:fullScreenElement 注意:现各大主流浏览器中由于内核不同的 ...

  6. 取消Fetch API请求

    如今,Fetch API已经成为现在浏览器异步网络请求的标准方法,但Fetch也是有弊端的,比如: Fetch还没有方法终止一个请求,而且Fetch无法检测上传进度 现在我们可以通过 AbortCon ...

  7. css 中可以继承的属性

    CSS中可以和不可以继承的属性一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到 ...

  8. 如何使用活字格快速搭建Bug管理系统?

    Bug管理系统是指一种用于添加Bug.修复Bug.测试Bug.删除Bug的一套完整的Bug管理系统. 完整的Bug管理过程包含: 1.测试人员利用Bug管理系统提交发现的bug. 2.测试人员把bug ...

  9. 2018-10-27 22:44:33 c language

    2018-10-27  22:44:33 c language 标准的C语言并不支持上面的二进制写法,只是有些编译器自己进行了扩展,才支持二进制数字.并不是所有的编译器都支持二进制数字,只有一部分编译 ...

  10. VMware 15 安装 MAC OS 10.13 原版(详细图文教程)

    VMware 15 安装 MAC OS 10.13 原版(详细图文教程) 生命在于折腾,之前本想装个双系统黑苹果,什么 U 盘启动盘,四叶草引导,都配置好了,最后跪在一个动态卷上,备份格盘现在弄不了, ...