HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort)
题意分析
裸的拓扑排序
详解请移步
算法学习 拓扑排序(TopSort)
这道题有一点变化是要求计算最后的金钱数。最少金钱值是888,最少的差额是1,如1号的人比2号钱要多,2号至少是1号人的钱数+1.根据拓扑排序的思想,我们只需要类比二叉树的层序遍历,每次取出入度为0的节点做处理,累加金钱数量,然后再取出入度为0的节点处理。直到取出所有的点或者判断成环即可。
代码总览
#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define nmax 10005
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int n,m;
int indegree[nmax];
vector<int> v[nmax];
int cnt = 0;
int tot = 0;
bool suc = true;
void topsort()
{
queue<int> q;
int cal = 0;
while(1){
for(int i = 1;i<=n;++i){
if(indegree[i] == 0){
q.push(i);
cnt++;
tot+=888+cal;
indegree[i] = -1;
}
}
if(q.empty()) break;
while(!q.empty()){
int t = q.front();q.pop();
for(int i = 0; i<v[t].size();++i){
int tt = v[t][i];
if(indegree[tt] == -1){
suc = false;
break;
}else
indegree[tt] --;
}
if(!suc) break;
}
if(!suc) break;
cal++;
}
if(suc && cnt != n) suc = false;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) !=EOF){
suc = true;MEM(indegree);cnt = 0;tot = 0;
for(int i = 1; i<=n;++i) v[i].clear();
for(int i = 0; i<m;++i){
int a,b;
scanf("%d%d",&a,&b);
indegree[a]++;
v[b].push_back(a);
}
topsort();
if(suc){
printf("%d\n",tot);
}else
printf("-1\n");
}
return 0;
}
HDU.2647 Reward(拓扑排序 TopSort)的更多相关文章
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
随机推荐
- java中的比较:instanceof、equals(hashcode)、==
import javassist.expr.Instanceof; class Person{ String s; Person(String s){ this.s=s; } } class Man ...
- 在VMware虚拟机下安装Linux CentOS7
1.首先下载并安装VMware虚拟机,下载地址:https://www.vmware.com/cn/products/workstation-pro/workstation-pro-evaluatio ...
- Mac环境下RabbitMq安装与测试教程
RabbitMq安装与测试教程 Installing on Mac I. 安装 123456789 brew install rabbitmq ## 进入安装目录cd /usr/local/Cella ...
- TPO-14 C1 Locate a political book
TPO-14 C1 Locate a political book 第 1 段 1.Listen to a conversation between the student and librarian ...
- 【WXS】变量定义保留标识符
以下字符不能作为变量名称定义: delete void typeof null undefined NaN Infinity var if else true false require this f ...
- CSP201312-2:ISBN号码
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- 数据库Mysql的学习(五)-运算符与函数
,store,store,store,store FROM bookinfo;//加减乘除取余 //余额大于200 //余额不等于200 SELECT * FROM readerinfo WHERE ...
- Too many open files错误与解决方法
致前辈:该问题的解决思路给了我很大的启发,文章作者Lis, Linux资深技术专家. 问题现象:这是一个基于Java的web应用系统,在后台添加数据时提示无法添加,于是登陆服务器查看Tomcat 日志 ...
- selenium实现文件上传方法汇总(AutoIt、win32GUI、sengkeys)---基于python
在使用selenium进行UI自动化测试时,经常会遇到一个关于本地文件上传的问题,解决此问题一般分两种情况: 1. 元素标签为input 2.非input型上传 下面我们分别对着两种情况进行实例分析 ...
- java — 值传递和引用传递
在 Java 应用程序中永远不会传递对象,而只传递对象引用.因此是按引用传递对象.Java 应用程序按引用传递对象这一事实并不意味着 Java 应用程序按引用传递参数.参数可以是对象引用,而 Java ...