杭电 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 ...
随机推荐
- POJ 2104 K-th Number && 洛谷 P3834 【模板】可持久化线段树 1(主席树)
我惊奇的发现这两道题一模一样 题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询 ...
- c++继承汇总(单继承、多继承、虚继承、菱形继承)
多重继承中,一个基类可以在派生层次中出现多次,如果一个派生类有多个直接基类,而这些直接基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类数据成员的多分同名成员.C++提供虚基类的方法使得在 ...
- JSP九大内置对象的作用和用法总结【转】
JSP九大内置对象的作用和用法总结? JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.c ...
- RCC 2014 Warmup (Div. 1)
A 暴力 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- 什么是极坐标? —— 一点微小的想法 What is Polar Coordinate ? - Some Naive Thoughts about It
Can you answer these three questions? The answer seems to be trivial, since we can use our eyes to o ...
- LN : leetcode 516 Longest Palindromic Subsequence
lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...
- 进程间通信的两种实现方式(IPC)
进程间通信的两种实现方式(IPC) IPC: iter processing communicate 进程间通信:IPC(iter process communicate)linux free-m 可 ...
- 序列化shelve模块
1.shelve对pickle进行封装,所以shelve也只能在python里使用. shelve可以进行多次dump而且顺序不会乱. import shelve f = shelve.open('s ...
- 初次使用引用外部js心得
在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...
- python pandas 中 loc & iloc 用法区别
转自:https://blog.csdn.net/qq_21840201/article/details/80725433 ### 随机生DataFrame 类型数据import pandas as ...