杭电 2647 Reward (拓扑排序反着排)
Description
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
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1 大意:
农场主要发工资,但是有一定规则,有些人得工资要比一些人高,每个人工资最低是888.先输入两个整数n,m,代表工人的数量编号1~n和m种规则,接下来的m行每行有两个整数a,b表示a的工资比b高,输入最小钱数,若不存在输出-1。 因为无法确定工资最高的人工资是多少,所以反着排序,让工资最小的在前面,输入a,b时记录b约束a,前驱为0的人工资为888,依次累加。
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int n,m,i,j,num[],head[],mon[],sum,ans;
struct stu
{
int to,next;
}st[];
void init()
{
memset(num,,sizeof(num));
memset(head,-,sizeof(head));
memset(mon,,sizeof(mon));
sum=;
ans=;
}
void add(int a,int b)
{
st[i].to=b;
st[i].next=head[a];
head[a]=i;
num[b]++;
}
void topo()
{
int i,j;
queue<int>que;
while(!que.empty())
{
que.pop();
}
for(i = ; i <= n ; i++)
{
if(num[i] == )
{
mon[i]=;
que.push(i);
}
}
while(!que.empty())
{
ans++;
m=que.front();
que.pop();
sum+=mon[m];
for(i = head[m] ; i != - ; i = st[i].next)
{
if(--num[st[i].to] == )
{
que.push(st[i].to);
mon[st[i].to]=mon[m]+;
}
}
}
if(ans == n)
printf("%d\n",sum);
else
printf("-1\n");
}
int main()
{
int a,b;
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
for(i = ; i < m ; i++)
{
scanf("%d %d",&a,&b);
add(b,a);
}
topo();
} }
杭电 2647 Reward (拓扑排序反着排)的更多相关文章
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- 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(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- Reward 杭电 2647
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- hdu2647 Reward 拓扑排序
此题的关键在于分层次,最低一层的人的奖金是888,第二层是888+1 …… 分层可以这样实现.建立反向图.在拓扑排序的时候,第一批入度为0的点就处于第一层,第二批处于第二层 …… 由于是逐个遍历入度为 ...
- 杭电2000——ASCII码排序
/* ASCII码排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- mycat启动报错UnknownHostException(Temporary failure in name resolution)解决方法
重启命令 ./mycat restart 查看日志 cd logs tail -f wrapper.log 报错信息 INFO | jvm 2 | 2018/05/09 11:28:28 | Erro ...
- 接口测试postman和Jmeter
接口测试属于功能测试的范畴,一般来说分为两种,一种是程序内部的接口,一种是系统对外的接口. postman测试接口 1. get请求: (1) 直接在浏览器中输入接口地址+?+参数 (2)使用post ...
- 18.3.2从Class上获取信息(方法)
package d18_3_1; import java.lang.reflect.Method; import java.util.Arrays; /** * 获取Class对应类所包含的方法的四个 ...
- (转)深入理解Java对象的创建过程
参考来源:http://blog.csdn.net/justloveyou_/article/details/72466416 摘要: 在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一 ...
- subline应用之常用插件
汉化插件:ChineseLocalzations IMESupport插件:解决软件输入法跟随问题 SublimeREPL插件:可以用于运行和调试一些需要交互的程序如python SublimeCod ...
- 前端之CSS创建的样式
CSS即层叠样式表,在创建时有以下几种样式: 1.内联样式(行内样式.行间样式): <标记 style=“属性:属性值:”></标记> 2.内部样式(嵌入式样式): <s ...
- jQuery选择器之表单元素选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- ARM 环境下使用azure powershell 从远程blob中拉去vhd 并创建虚拟机
最近需要从指定公共访问的blob中复制vhd到自己的订阅存储账户,并使用vhd创建AZURE ARM虚拟机(非经典版),而且在portal.azure.cn中无法实现虚拟机映像创建等功能,于是自己使用 ...
- 杨辉三角python的最佳实现方式,牛的不能再牛了
def triangles(): N = [1] while True: yield N N.append(0) N = [N[i-1] + N[i] for i in range(len(N))] ...
- 在Servlet中使用@Autowire的方法
在你调用的Servlet中添加如下代码: public void init(ServletConfig config) { try { super.init(config); SpringBeanAu ...