HDOJ 2647 Reward 【逆拓扑排序+分层】
题意:每一个人的基础工资是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 【逆拓扑排序+分层】的更多相关文章
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward(拓扑排序,vector实现邻接表)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 杭电 2647 Reward (拓扑排序反着排)
Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- Leetcode207--->课程表(逆拓扑排序)
题目: 课程表,有n个课程,[0, n-1]:在修一个课程前,有可能要修前导课程: 举例: 2, [[1,0]] 修课程1前需要先修课程0 There are a total of 2 courses ...
- HDU 2647:Reward(拓扑排序+队列)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Reward(拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意: 老板要给n个员工发工资最低工资是888: 但是工人们是有要求的 如果输入 a b 表示a的工资要比 ...
随机推荐
- JS函数种类详解
1. 普通函数1.1 示例 1 2 3 function ShowName(name) { alert(name); } 1.2 Js中同名函数的覆盖 在Js中函数是没有重载,定义相同函数名.不同 ...
- shp系列(四)——利用C++进行Shx文件的读(打开)
1.shx文件的基本情况 shx文件又叫索引文件,主要包含坐标文件的索引信息,文件中每个记录包含对应的坐标文件记录距离坐标文件的初始位置的偏移量.通过索引文件可以很方便地在坐标文件中定位到指定目标的坐 ...
- B - Calculating Function
Problem description For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( ...
- Data内置对象
1.内置对象 Date 日期对象 2.创建日期对象 2.1 根据当前的系统时间来创建日期对象. var date1 = new Date(); //a.输出日期对象的信息 console.log(da ...
- 使用jQuery对图片进行居中设置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MAVEN学习笔记之基础(1)
MAVEN学习笔记之基础(1) 0.0 maven文件结构 pom.xml src main java package resource test java package resource targ ...
- Windows Phone - 按钮/button 控件
System.Windows.Controls.Button button控件一.button控件的各种样式的展示可以通过 …… 来给控件定义公共的样式调用样式的方法:在Button控件上添加样式 ...
- WP - 控件基础-按钮控件
Button:HyperlinkButton:RepeatButton:ToggleButton 1.Button: <button content="Button" ...
- C# window服务操作
public int GetWindowsServiceStartType(String sServiceName) //判断服务状态是手动还是禁用还是自动 { string sState = &qu ...
- iOS 小知识
iOS 各版本: http://www.pig66.com/2018/145_1021/17357553.html