题意:每一个人的基础工资是888。 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多。问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1.

策略:拓扑排序。

这道题有些难点:一:数据大,建二维数组肯定不行,要换其它的数据结构(vector, 或者是链式前向星(本题代码用的是链式前向星)); 二:要逆拓扑排序(就是将++in[b]换成++in[a])。 三要分层次(依据上一个的钱数+1就可以)。

不懂什么是链式前向星 移步:http://blog.csdn.net/acdreamers/article/details/16902023

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAXN 10005
int head[MAXN*2];
struct EdgeNode{
int to;
int next;
};
EdgeNode edges[MAXN*2];
int in[MAXN], queue[MAXN], money[MAXN];
int n, ans;
int toposort()
{
ans = 0;
memset(money, 0, sizeof(money));
int i, j;
int iq = 0;
for(i = 1; i <= n; i ++){
if(!in[i]){
queue[iq++] = i;
}
}
for( i = 0; i < iq; i ++){
int temp = queue[i];
ans += money[temp];
for(j = head[temp]; j != -1; j = edges[j].next){
if(!--in[edges[j].to]){
queue[iq++] = edges[j].to;
money[edges[j].to] = money[temp]+1;//这里是分层次。
}
}
}
return iq == n;
}
int main()
{
int m, i, a, b;
while(scanf("%d%d", &n, &m) == 2){
memset(head, -1, sizeof(head));
memset(in, 0, sizeof(in));
for(i = 0; i < m; i ++){
scanf("%d%d", &a, &b);
in[a]++;
edges[i].to = a;
edges[i].next = head[b];
head[b] = i;
}
int sum = 888*n;
int flag = toposort();
printf("%d\n", flag?sum+ans:-1);
}
return 0;
}

题目链接:

pid=2647">http://acm.hdu.edu.cn/showproblem.php?pid=2647

HDOJ 2647 Reward 【逆拓扑排序+分层】的更多相关文章

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

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

  2. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  3. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

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

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

  5. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  6. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  7. Leetcode207--->课程表(逆拓扑排序)

    题目: 课程表,有n个课程,[0, n-1]:在修一个课程前,有可能要修前导课程: 举例: 2, [[1,0]] 修课程1前需要先修课程0 There are a total of 2 courses ...

  8. HDU 2647:Reward(拓扑排序+队列)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. Reward(拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意: 老板要给n个员工发工资最低工资是888: 但是工人们是有要求的 如果输入 a b 表示a的工资要比 ...

随机推荐

  1. Python关于super()函数的理解

    看下面的例子: class A: def __init__(self, name): self.name = name def bb(self): print('没事就爱瞎BB') class B(A ...

  2. C - Unary(map)

    Problem description Unary is a minimalistic Brainfuck dialect in which programs are written using on ...

  3. vue开发的项目中遇到的警告,报错,配置项目文件等合集(长期更新)

    1. Vue组件里面data()里面没有return时触发错误:Vue components Cannot read property '__ob__' of undefined 这个警告不解决会触发 ...

  4. 5个对话框和FileStream:文件流

    1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...

  5. py2exe打包OpenCV,找不到libiomp5md.dll

    问题:py2exe打包OpenCV,找不到libiomp5md.dll 解决方法:把 libiomp5md.dll 从numpy/core/ 里面复制到 python27/DLLS/文件夹!!!

  6. OnLineML:时序数据挖掘

    关于时序分析: 我们跟随时间的脚步,试图解释现在.理解过去.甚至预测未来........ 原文链接:http://blog.sciencenet.cn/home.php?mod=space&u ...

  7. "国学"能带给孩子什么?

       您是家长吗?   是!就点标题下方蓝色的 静心坊 三字,然后点关注!您就可以享受到我们为您提供的最新教育信息. 读国学,除大量识字之外,还能带给孩子什么呢? 男孩子读国学,国学能教会他做一个正直 ...

  8. 创建一个dynamics CRM workflow (四) - Development of Custom Workflows

    首先我们需要确定windows workflow foundation 已经安装. 创建之后先移除MyCustomWorkflows 里面的 Activity.xaml 从packages\Micro ...

  9. 文字横向滚动marquee

    <div style="width:200px; height:300px"> <marquee behavior="scroll" cont ...

  10. Ubuntu Server 18.04 LTS 安装

    版本:Ubuntu Server 18.04.1 LTS 环境:VMware Workstation 14 Player 下载地址:https://www.ubuntu.com/download/se ...