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. js 下拉加载

    // 下拉加载    var clientHeight = $(window).height() //当前可视的页面高度    console.log(clientHeight) //滚动条到页面底部 ...

  2. django-templates过滤器

    常用内置过滤器: 过滤器会更改量或便签参数的值: title过滤器: {{ django|title }} 在下列context中 {'django': 'the web framework for ...

  3. MySQL中使用连接查询

    连接查询: 将多张表(可以大于2张)进行记录的连接(按照某个指定的条件进行数据拼接): 最终结果是: 记录数有可能变化, 字段数一定会增加(至少两张表的合并)! 连接查询的意义: 在用户查看数据的时候 ...

  4. layer弹出框确定前验证:弹出消息框(弹出两个layer)

    作者QQ:1095737364 QQ群:123300273 欢迎加入! layer 弹出框中经常遇到要弹出表单进行修改数据, 因此在弹出框中的表单需要验证数据, 就需要在弹出一个layer, 默认的设 ...

  5. js-ES6学习笔记-async函数(3)

    1.await命令后面的Promise对象,运行结果可能是rejected,所以最好把await命令放在try...catch代码块中. 2.多个await命令后面的异步操作,如果不存在继发关系,最好 ...

  6. 用eclipse 搭建struts2环境

    一,下载struts2对应的jar包,(http://struts.apache.org/download.cgi#struts2514.1),我一般下载struts2.3版本的 二,打开eclips ...

  7. 用php和ajax写一个省市区的三级联动,实现地区的下拉选择

    要实现这个页面的三级联动,我们需要建立三个php文件,第一个php文件我们导入jQuery文件,里面嵌入JavaScript:第二个php文件我们做一个php的处理页面,里面引入我们封装好的数据库类文 ...

  8. JetBrains PhpStorm 2017.2 x64 激活

    使用方法:激活时选择License server 填入http://idea.imsxm.com 点击Active即可

  9. using 和try/catch区别和注意点

    书上解释: using: 在C#和其他托管语言中,没有自动.决定性的析构方式,而是有一个垃圾收集器,它会在未来的某个时刻释放资源.它是非决定性的,因为我们不能确定这个过程在什么时候发生.忘记关闭数据库 ...

  10. guid是否为空的判断

    Guid类型的变量不会为空,初始化没有赋值的GUID应该是00000000-0000-0000-0000-000000000000 . 正确的判断应该是if(Guid testId== Guid.Em ...