HDU 2647 Reward(图论-拓扑排序)
Reward
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
2 1
1 2
2 2
1 2
2 1
1777
-1
题目大意:
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(图论-拓扑排序)的更多相关文章
- 题解报告: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 ...
- 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 ...
- 杭电 2647 Reward (拓扑排序反着排)
Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- HDU 2647:Reward(拓扑排序+队列)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- Asp.net获取用户名和IP
1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:Page.Request.Use ...
- Asp.net MVC4 +EF6开发
Asp.net MVC4 +EF6开发的个人网站源码和介绍(仅供新手学习) 本项目是我去年利用业余时间开发的,采用的是asp.net mvc 4 +EF6+三层架构,适合新手进行学习,高手就没有什么价 ...
- RH033读书笔记(5)-Lab 6 Exploring the Bash Shell
Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...
- [Linux]scp 命令远程复制
这些天来干预系统之前没有接触,建立使用用途良好的发展环境 scp命令,那么如何使用查询以下信息. scp是secure copy的缩写.主要用来linux系统之间的文件和文件夹的远程拷贝 能够非常ea ...
- Lua之Lua数据结构-TTLSA(6)(转) good
一. tabletable是lua唯一的数据结构.table 是 lua 中最重要的数据类型. table 类似于 python 中的字典.table 只能通过构造式来创建.其他语言提供的其他数据结构 ...
- 编译命令行终端 swift
So, this is where swift lives, after you've installed XCode 6 Beta: /Applications/Xcode6-Beta.app/Co ...
- Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记
面前app当完成测试,没问题,以完成整个老龄化阶段包含数据收发器,关键在 adb shell top -m 5 我发现我的 app pid 占用 CPU是最多的,事实上我想说写一个app是不难,你 ...
- Hosting WCF Service
之前在博客几个实例DemoWCF服务寄宿到控制到应用程序中,这篇来总结一下,经常使用的几种宿主的方式. 1.Self-Hosting 一个WCF服务可以寄宿在控制台应用程序或者WinForms app ...
- Hadoop入门进阶步步高(五)-搭建Hadoop集群
五.搭建Hadoop集群 上面的步骤,确认了单机能够运行Hadoop的伪分布运行,真正的分布式运行无非也就是多几台slave机器而已,配置方面的有一点点差别,配置起来就很easy了. 1.准备三台se ...
- R语言和数据分析十大:购物篮分析
提到数据挖掘,我们的第一个反应是之前的啤酒和尿布的故事听说过,这个故事是一个典型的数据挖掘关联规则.篮分析的传统线性回归之间的主要差别的差别,对于离散数据的相关性分析: 常见的关联规则: 关联规则:牛 ...