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. oracle 优化or 更换in、exists、union all几个字眼,测试没有问题!

    oracle 优化or 更换in.exists.union几个字眼.测试没有问题! 根据实际情况选择相应的语句是.假设指数,or全表扫描,in 和not in 应慎用.否则会导致全表扫描.  sele ...

  2. android AlarmManager采用

    Android的闹钟实现机制非常easy, 仅仅须要调用AlarmManager.Set()方法将闹钟设置提交给系统,当闹钟时间到后,系统会依照我们的设定发送指定的广播消息.我们写一个广播去接收消息做 ...

  3. GUI (图形界面)知识点

    一:组件知识点 JTextField:    作用:  定义文本域,只支持单行输入.                使用:  定义文本域:  JTextField jtf=new JTextField ...

  4. Encountered a section with no Package: header

    刚才打开ubuntu,我的版本号是11.04.正想打开新立得软件工具包更新软件的时候,出现了例如以下错误: E:Encountered a section with no Package: heade ...

  5. 华为OJ:查找字符的第一个字符串只出现一次

    您可以使代码有点写得真好,不要直接写双循环,如果,是可能的写函数调用,非常高的可重用性. import java.util.Scanner; public class findOnlyOnceChar ...

  6. 【应用篇】Activiti显示器(抽象)简单的应用程序和服务的颗粒结合(两)

    Activiti简单的应用程序,业务颗粒与工作流程结合.让流程带动业务颗粒运行的过程.此次的监听我们应用抽象的监听来实现,也就是说全部的普通业务类均应用此抽象监听,而不须要每个类一个监听的来操作. 新 ...

  7. Solr/SolrCloud -error

    状态 2014-08-20 10:46:22,356 INFO [coreZkRegister-1-thread-1] [org.apache.solr.cloud.ShardLeaderElecti ...

  8. CSDN上看到的一篇有关Spring JDBC事务管理的文章(内容比较全) (转)

    JDBC事务管理 Spring提供编程式的事务管理(Programmatic transaction manage- ment)与声明式的事务管理(Declarative transaction ma ...

  9. Jsoup一个简短的引论——采用Java抓取网页数据

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40115479 概述 jsoup 是一款Java 的HTML解析器,可直接解析某个U ...

  10. Windowsport80解决方案被占用

    今天,在一个非常沮丧的实施Server什么时候,一个错误port80占用.因此,找到一种方法来解决各类.最后,我的解决方案列出的问题来,要遇到的人做一些参考同样的问题. 第一步,找出哪些程序正在使用p ...