hdoj 2647 Reward【反向拓扑排序】
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5617 Accepted Submission(s):
1707
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.
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.
uncle needs to distribute .If it's impossible to fulfill all the works' demands
,print -1.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#define MAX 20010
using namespace std;
vector<int>map[MAX];
int vis[MAX];
int reward[MAX];
int n,m,sum;
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
map[b].push_back(a);
vis[a]++;
}
}
void tuopu()
{
int i,j,ok=0,sum=0;
queue<int>q;
memset(reward,0,sizeof(reward));
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
{
q.push(i);
reward[i]=888;
}
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
q.pop();
ans++;
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
{
q.push(v);
reward[v]=reward[u]+1;
}
}
}
if(ans!=n)
printf("-1\n");
else
{
for(i=1;i<=n;i++)
sum+=reward[i];
printf("%d\n",sum);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getmap();
tuopu();
}
return 0;
}
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 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- 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 ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- 逃生(HDU4857 + 反向拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...
- HDU-4857-逃生-反向拓扑排序+优先队列
HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...
随机推荐
- 系统设计 - IOS 程序插件及功能动态更新思路
所用框架及语言 IOS客户端-Wax(开发愤怒的小鸟的连接Lua 和 Objc的框架),Lua,Objc, 服务端-Java(用于返回插件页面) 由 于Lua脚本语言,不需要编译即可运行 ...
- [转]看看国外的javascript题目,你能全部做对吗?
叶小钗 的博客最近都在讨论面试题目 正好以前也看过一篇,就借花献佛拿出来分享一下 http://perfectionkills.com/javascript-quiz/ 附带了自己的理解,答案有争议的 ...
- [LeetCode OJ] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Linux 权限基础说明
1 权限位说明 Linux文件或目录的权限位是由个9个权限位来控制的,每三位为一组,它们分别是文件属主(owner/user)读.写.执行,用户组(Group)的读.写.执行以及(Other)其他 ...
- struts2 package元素
<package../>元素 name 必选 包名 extends 可选 继承 namespace ...
- Linux(Fedora)下NodeJs升级最新版本(制定版本)
Linux(Fedora)下NodeJs升级最新版本(制定版本) 首先安装n模块: npm install -g n 升级node.js到最新稳定版 n stable 升级node.js到制定版本 n ...
- AS3.0面向对象的写法,类和实例
package /*package是包路径,例如AS文件在ActionScript文件夹下,此时路径应为package ActionScript.必须有的.package中只能有一个class,在一个 ...
- 利用jquery进行ajax提交表单和附带的数据
1.获取表单数据: $form.serialize() 2.附带数据:input[status]=1 3.构造url链接:url = $form.attr('action') + '?input[st ...
- 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
1. VirtualBox的安装 参考常规安装方式即可. VirtualBox 4.3.14 for Windows hosts:http://download.virtualbox.org/virt ...
- 如何删除github上的项目
如果大伙用过github,那么就会知道它上面没有明显的选项可以让你直接删除掉一个Repositories或者项目.因此对很多github的新手来说一开始也常觉得这个事情比较犯难.这里我给大家简单介绍一 ...