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. SQL中如何使用方向键——lrwrap

    Linux alias命令用于设置指令的别名. 用户可利用alias,自定指令的别名.若仅输入alias,则可列出目前所有的别名设置.alias的效力仅及于该次登入的操作.若要每次登入是即自动设好别名 ...

  2. app 爬虫

    https://mp.weixin.qq.com/s/ClYYfpvylQGlYYHDFBuKpA 唯品会舆情监控系统 姚彬炎 唯技术 2月26日  

  3. 使用docker运行mysql

    以前开发的时候都是用本地的sqlite开发,但是极少数情况下,sqlite支持的语法发布到服务器上链接mysql会报错. 为了避免这种现象,还是链接本地mysql开发还是更稳定的, 可是开发的项目多了 ...

  4. hwclock和date源码分析

    一. hwclock 1.1 hwclock源码在哪里? util-linux 或者busybox 1.2 获取源码 git clone https://github.com/karelzak/uti ...

  5. CV3——学习笔记-实战项目(上):如何搭建和训练一个深度学习网络

    http://www.mooc.ai/course/353/learn?lessonid=2289&groupId=0#lesson/2289 1.AlexNet, VGGNet, Googl ...

  6. ps 快捷键大全

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...

  7. 【416】Ubuntu 配置

    修改桌面主题:How to Install Desktop Themes on Ubuntu 18.04 LTS 命令行显示短路径:怎样ubuntu下命令行终端显示短路径 gedit 扩展插件:Use ...

  8. C# using语句 中间出现exception后 using仍然会dispose

    C# using语句,中间出现exception后,using仍然会dispose,因为: using语句 相当于 try-finally{dispose()}

  9. 从成员函数指针生成可调用对象:function<>、mem_fn()和bind()

    我们知道,普通函数指针是一个可调用对象,但是成员函数指针不是可调用对象.因此,如果我们想在一个保存string的vector中找到第一个空string,不能这样写: vector<string& ...

  10. Hibrtnate组件映射

    Component映射 在hibernate中Component映射采用<component>标签即可 Component是某个实体的逻辑组成部分,它与实体类的主要差别在于,它没有oidC ...