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. Atitit.列表页and查询条件的最佳实践(1)------设定搜索条件and提交查询and返回json数据

    Atitit.列表页and查询条件的最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段显示类 ...

  2. 【ThinkingInC++】53、构造函数,析构函数,全局变量

    /** * 图书:[ThinkingInC++] * 特征:构造函数,析构函数,全局变量 * 时刻:2014年9一个月17日本18:07:43 * 笔者:cutter_point */ #includ ...

  3. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  4. org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/jiaoshi/daochuEx

    org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/jiaoshi/daochuEx ...

  5. 【Android基础】eclipse常用快捷键

    Alt+/ 内容辅助键--列出相关内容 Ctrl+shift+o 导入所需的所有类包 Ctrl+1 对现有错误的n中解决方案 Crtl+d 删除所在行或者全部 CTRL+alt+上下键 上下复制 Cr ...

  6. Linux IO工具 iotop备择方案iopp

    iotop毫无疑问linux IO检测上是一个很好的工具,但苦于要求和内核版本Python版本号.我的很多朋友放弃了.我也是.无意中发现iopp,使用c书面,与此iotop它是一个作用.nice! 一 ...

  7. Java实现 Base64、MD5、MAC、HMAC加密(转)

    开始对那些基本的加密还不怎么熟练,然后总结了些,写了一个测试:支持 Base64.MD5.MAC.HMAC加密,长话短说,我们都比较喜欢自己理解,看代码吧! 采用的输UTF-8的格式... packa ...

  8. 聪明的kk

    聪明的kk 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 聪明的"KK" 非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘.体现出本 ...

  9. Linux进程管理(-)

    一.进程的类型 能够将执行在Linux系统中的进程分为三种不同的类型: 交互进程:由一个Shell启动的进程.交互进程既能够在前台执行,也能够在后台   执行. 批处理进程:不与特定的终端相关联,提交 ...

  10. js 自己容易搞混的笔记查询

    相似的操作 var str2 = "0123456789"; console.log(str2.slice(4,7)); //------------"456" ...