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. RK3399 focaltech敦泰触摸屏移植调试

    CPU:RK3399 系统:Android 7.1 IC:FT5406 focaltech(敦泰)触摸屏也是比较常用的,但是相对汇顶,就比较少用 RK的源码中虽然有 focaltech 的代码,但没有 ...

  2. SDK/JDK,Shell/Shell脚本,Apache/APR ,MTK

    SDK 软件开发工具包(SoftwareDevelopmentKit) API(Application Programming Interface,应用编程接口)其实就是操作系统留给应用程序的一个调用 ...

  3. HBase里配置SNAPPY压缩以后regionserver启动不了的问题

    配置了HBase的SNAPPY压缩以后,出现regionserver启动不了的问题.分析应该是属性配置错了! 官网上的是:<name>hbase.regionserver.codecs&l ...

  4. JavaScript——closures(待续)

    问答原文:How do JavaScript closures work?

  5. 解析python 命令的-u参数

    在shell脚本中运行python 命令时后面加了-u 参数(python -u xx.py),这个-u表示什么? import sys sys.stdout.write("stdout1& ...

  6. 123457123456#0#-----com.twoapp.mathGame13--前拼后广--13种数学方法jiemei

    com.twoapp.mathGame13--前拼后广--13种数学方法jiemei

  7. PostgreSQL学习笔记——摘要

    因为PostgreSQL和MySQL.DB2等数据库均遵循SQL语法,所以这篇随笔仅记录一些PostgreSQL中和别的数据库有差别或之前学习中遗漏的地方,以及一些我觉得比较重点的地方. 通过psql ...

  8. SecureCRT 8.1破解方式

    百度网盘下载,里面有破解程序和破解方式. 链接: https://pan.baidu.com/s/1wlhqnn-TE_mcOXOLljP-zg 密码: 3ffj

  9. Node.js中使用pipe拷贝大文件不能完全拷贝的解决办法

    原来的代码如下: var readable = fs.createReadStream( filepath ); var writable = fs.createWriteStream( outFil ...

  10. Python3之返回函数

    参考:https://www.cnblogs.com/mzc1997/p/7641995.html Python中函数不仅可以作为参数还可以作为结果返回 >>> def pro1(c ...