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. 字节组数(二进制流)、Base64、图片(文件)、二进制相互之间转换

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...

  2. arcgis python 刷新

    arcpy.RefreshActiveView() 刷新地图和布局窗口 arcpy.RefreshTOC() 刷新内容列表 arcpy.RefreshCatalog(r"F:\tknew10 ...

  3. 猎豹网校C++ Primer学习笔记

    1.头文件(15th课) 大型项目开发,要有很多头文件.只能写声明,不能定义(类定义和常量定义可以). 自己新建头文件(类定义,外部变量声明,函数声明).源文件包含对应的头文件. 头文件里写类的声明, ...

  4. MindManager2018试用期过后 修改过期时间 破解使用

    MindManager2018试用期过后 修改过期时间 破解使用 2019年06月13日 15:58:11 一生中所爱 阅读数 1991更多 分类专栏: 工具软件使用   1.找到路径:C:\User ...

  5. 网站url路径优化方法完全讲解 (url优化、基于tp5、API接口开发)

    url优化可是网站开发的必备高阶技能,先看本实例优化前后效果比较: (同为调用前台模块下的index控制器下的index方法) 优化前:www.tp5.com/tp5/public/index.php ...

  6. 集合循环删除问题-报错java.util.ConcurrentModificationException解析

    java.util.ConcurrentModificationException 异常问题详解 环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修 ...

  7. fiddler https 抓包

    手机上https抓包: http://blog.csdn.net/wangjun5159/article/details/52202059 需要给fiddler安装插件CertMaker for iO ...

  8. 跟我学Shiro目录贴

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2018398 扫一扫,关注我的公众号 购买地址 历经三个月左右时间,<跟我学Shiro&g ...

  9. URL锚点HTML定位技术机制、应用与问题

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=3591 一.锚点是什么 ...

  10. Netty学习笔记(一)——nio基础

    Netty简单认识: 1) Netty 是由JBOSS 提供的一个Java 开源框架. 2) Netty 是一个异步的.基于事件驱动的网络应用框架,用以快速开发高性能.高可靠性的网络I0 程序. 3) ...