Reward

Problem Description
Dandelion's uncle is a boss of a factory. As the spring 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.
 
Input
One line with two integers n and m ,stands for the 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.
 
Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
 
Sample Input
2 1
1 2
2 2
1 2
2 1
 
Sample Output
1777
-1
 
Author
dandelion
 
Source
 
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  1285 3342 1811 2680 2112 
 

题目大意:

n个人,m条边,每条边a,b 表示a比b的工资多1,每一个人的工资至少888,问你工资和至少多少?假设出现矛盾关系,输出-1

解题思路:

依据人的工资关系建立拓扑图,工资尽量从888開始,然后依据能否所有排好序推断是出现矛盾关系。

解题思路:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int maxn=11000;
int n,m,ans[maxn],r[maxn];
vector <vector<int> > v; void input(){
v.clear();
v.resize(n+1);
for(int i=0;i<=n;i++){
ans[i]=-1;
r[i]=0;
}
int a,b;
while(m-- >0){
scanf("%d%d",&a,&b);
v[b].push_back(a);
r[a]++;
}
} void solve(){
queue <int> q;
for(int i=1;i<=n;i++){
if(r[i]==0) q.push(i);
}
int tmp=888;
while(!q.empty()){
int qsize=q.size();
while(qsize-- >0){
int s=q.front();
q.pop();
ans[s]=tmp;
for(int i=0;i<v[s].size();i++){
int d=v[s][i];
r[d]--;
if(r[d]==0) q.push(d);
}
}
tmp++;
}
int sum=0;
for(int i=1;i<=n;i++){
if(ans[i]>0) sum+=ans[i];
else{
sum=-1;
break;
}
}
printf("%d\n",sum);
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
input();
solve();
}
return 0;
}

HDU 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. hdu 2647 Reward(拓扑排序,反着来)

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

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

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

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

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

  7. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  8. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  9. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

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

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

随机推荐

  1. nyoj 47 江 河问题 【贪婪】

    经典的贪婪. 两种方案:一个:让我们来最快,第二快,在过去的第一,最快的回.然后最慢,最慢第二,在过去.次最快的回来a[0]+a[1]+a[1]+a[n-1] 二:最快的和最慢的过去,最快的回来,最快 ...

  2. 如何处理 Windows Phone 8 动态砖变成黑白砖

    原文:如何处理 Windows Phone 8 动态砖变成黑白砖 ? 问题的来龙去脉 我的 Windows Phone 8 动态砖变成黑白砖,所有图示和文字变成黑白,该如何处理? ? 问题的发生原因 ...

  3. UVA - 12130 Summits

    Description Problem G - Summits Time limit: 8 seconds You recently started working for the largest m ...

  4. [Servlet3.0新功能]注释替代配置文件

    作者信息 作者名称:金云龙 个人网站:http://www.longestory.com 个人公众帐号:搜索"longestory"或"龙哥有话说" 须要注意的 ...

  5. JAVA环境配置---------jdk安装路径中存在空格的问题

    我把jdk 安装在C:\Program Files下 而ide启动脚本 startup.bat 例如以下: if "%OS%"=="Windows_NT" se ...

  6. Razor基础语法

    原文:Razor基础语法 一.介绍      Razor是ASP.NET MVC视图引擎的代号,支持.cshtml和.vbhtml两种模板文件,符号@标记服务端代码的开始,比以前asp.net的< ...

  7. 使用bootbox.js(二级务必提交书面和数字到数字中国)

    页面文件 <#-- 页头 --> <#assign currNav = "deposit"> <#assign title="网校充值&qu ...

  8. Mongodb安装和配置

    Mongodb之安装配置 安装 Mongodb的下载地址为Mongodb官网.下载时.你能够选择是安装包或者是压缩包. 下载完毕后.双击安装包并安装. 安装完毕后.你能够在安装文件夹看到下图中所见的文 ...

  9. VisualStudioOnline协同工作流程

    VisualStudioOnline协同工作流程 项目负责人登陆自己的vsonline新建项目就不多说了. 直接从邀请队友开始 项目负责人操作 被邀请的邮箱必须是微软的邮箱(也就是可以登录visual ...

  10. 第十七章——配置SQLServer(3)——配置“对即时负载的优化”

    原文:第十七章--配置SQLServer(3)--配置"对即时负载的优化" 前言: 在第一次执行查询或者存储过程时,会创建执行计划并存储在SQLServer的过程缓存内存中.在很多 ...