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
嗯。,。 这道题目是要统计费用 然后为了方便统计层数上的钱数  需要逆向构图
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 11000
#include<vector>
#include<iostream>
using namespace std;//拓扑排序的分层问题
int m,n,degree[maxn];
vector<int> mapp[maxn];//
int topo()
{

int
flag,sum=;
vector<int> ans;
int
tt=,re=;//tt表示层数
while
()
{

flag=;
ans.clear();
for
(int i=;i<=m;i++) if(degree[i]==)//每次得处理一层的 /./
{
ans.push_back(i);
sum+=(+tt);
degree[i]=-;
flag=;
re++;
}

if
(re>=m) return sum;//当遍历的点数到达m return
if
(flag==) return;
for
(int j=;j<ans.size();j++)
{

int
rett=ans[j];
for
(int k=;k<mapp[rett].size();k++)
{

int
temp=mapp[rett][k];
degree[temp]--;
}
}

tt++;
}

return
sum;
}

int
main()
{

int
a,b,i,j;
while
(scanf("%d%d",&m,&n)!=EOF)
{

if
(m==&&n==) break;
memset(degree,,sizeof(degree));
for
(i=;i<=m;i++) mapp[i].clear();
for
(i=;i<n;i++)
{

scanf("%d%d",&a,&b);
mapp[b].push_back(a);
degree[a]++;
}

int
t=topo();
if
(t==) printf("-1\n");
else
printf("%d\n",t);
}

return
;
}

hdu 2647 还是逆向拓扑的更多相关文章

  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. ACM: hdu 2647 Reward -拓扑排序

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

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

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

  8. 逆拓扑排序 Reward HDU - 2647

    Reward HDU - 2647 题意:每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 样例1: 2 1 1 ...

  9. HDU-4857 逃生(逆向拓扑排序)

    Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. ...

随机推荐

  1. Uploadifive 上传'fileType'格式如何限制?

    Uploadifive: 'fileType':[ "application/pdf", "application/msword", "applica ...

  2. 使用requirejs+vue 打造 无需编译发布便捷修改调整的模块开发方案 (一)

    前言 不知道大家有没有这种感觉,现在流行的很多前端技术,基本上都基于webpack编译,当然不是说这种方案不好,在标准的开发流程运行中,这种方式其实也挺不错,管理方便,代码统一. 痛点:项目不是单独针 ...

  3. python 设计模式之状态模式

    1.为什么会出现状态模式? 在软件开发过程中,各种应用程序可能会根据不同的情况做出不同的处理.最直接的方案就是把所有的可能发生的情况都考虑到.然后使用条件语句(if...elseif...elseif ...

  4. Docs-.NET-C#-指南-语言参考-预处理器指令:#if 预处理指令

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#if 预处理指令 1.返回顶部 1. #if(C# 参考) 2018/06/30 如果 C# 编译器遇到 #if 指令,最终是 ...

  5. 通过Redis的list来实现 Server - Client 的同步通信

    Redis实现类似同步方法调用的功能(一) 首先声明,这么干纯粹是为了好玩. 通常我们用Redis主要是为了存储一些数据,由于数据在内存里,所以查询更新很快.同时我们也可以利用 Pub/Sub 功能来 ...

  6. request cluster ID mismatch

    删除了etcd集群所有节点中的--data_dir的内容 [root@node3 ~]# cd /var/lib/etcd/ [root@node3 etcd]# ls member [root@no ...

  7. 常用SQL50句

    Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题 ...

  8. Apache配置日志切割

    随着网站的访问越来越大,WebServer产生的日志文件也会越来越大,如果不对日志进行分割,那么只能一次将大的日志(如Apache的日志)整个删除,这样也丢失了很多对网站比较宝贵的信息,因为这些日志可 ...

  9. 【产品】C转B

    http://www.woshipm.com/pmd/2780830.html 这篇文章总结的不错.

  10. 01.轮播图之五 :一个 imageView也能 作 轮播

    这个是最近才写的,本以为实现起来很有难度,需要更高深的理论, 写完之后,才发现自己错误的离谱: 之所以能用一个imageview 实现轮播 基于两点::: 使用 imageview 的layer 层设 ...