Reward

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

Sample Output

1777
-1

思路,构建反图,将工资高的放在后面,好比树,越深的工资越高,这里不算u->v,而是v->u;

无解当前仅当形成环,n个节点没有走完一遍。

代码:

#include <iostream>
#include<vector>
#include<queue>
using namespace std;
const int M=100005;
vector<int>G[M];
int in[M],num[M];
void topsort(int n){
int ans=0;
int cnt=0;//统计是否形成环,n个点全部输出则不可能形成环
queue<int>que;
//将入度为0 的压入队列
for (int i = 1; i <= n; i++){
if(in[i]==0)que.push(i);
}
while (!que.empty()){
cnt++;
int u=que.front();
que.pop();
ans+=num[u];
for (int i = 0; i <G[u].size(); i++){
int v = G[u][i];
if(--in[v]==0)que.push(v);
num[v]=num[u]+1;
}
}
if(cnt!=n)
cout<<-1<<endl;
else{
cout<<ans<<endl;
}
} int main(){
int n,m,u,v;
while (cin>>n>>m){
//初始化
for (int i = 1; i <= n; i++){
G[i].clear();
num[i]=888;
in[i]=0;
}
while (m--){
cin>>u>>v;
G[v].push_back(u);
in[u]++;
}
topsort(n);
}
}

HDU2647Reward (拓扑排序)的更多相关文章

  1. 逆拓扑排序 HDU2647Reward

    这个题如果用邻接矩阵的话,由于n比较大,会超内存,所以选用邻接表的形式.还有就是这个题有那个等级的问题,一级比一级的福利高,所以不能直接拓扑排序,而是反过来,计算出度,找出度为0的顶点,然后更新出度数 ...

  2. HDU2647-Reward(拓扑排序)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  4. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  7. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  9. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  10. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

随机推荐

  1. 一键部署lamp脚本

    #!/bin/bash systemctl stop firewalld systemctl disable firewalld setenforce 0 #-------Apache------ # ...

  2. DDD领域驱动理解

    在理解领域驱动的时候,网上很多大谈理论的文章,这种对于初学者不是太容易接受.根据我自己的学习经历,建议按照如下几个步骤学习: 粗略的看一遍领域驱动的理论,做到心中有形,知道领域驱动是什么,解决什么问题 ...

  3. 命令执行 安鸾 Writeup

    目录 命令执行 01 命令执行 02 仅代码层面来说,任意命令执行漏洞的利用条件: 1.代码中存在调用系统命令的函数 2.函数中存在我们可控的点 3.可控点没有过滤,或过滤不严格. Linux命令连接 ...

  4. [源码解析] 深度学习流水线并行Gpipe(1)---流水线基本实现

    [源码解析] 深度学习流水线并行Gpipe(1)---流水线基本实现 目录 [源码解析] 深度学习流水线并行Gpipe(1)---流水线基本实现 0x00 摘要 0x01 概述 1.1 什么是GPip ...

  5. 【UGUI源码分析】Unity遮罩之RectMask2D详细解读

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  6. Ubuntu完全卸载Docker步骤

    Ubuntu完全卸载Docker步骤:https://www.jianshu.com/p/c03044dbeaaf

  7. .NET Core:在ASP.NET Core WebApi中使用Cookie

    一.Cookie的作用 Cookie通常用来存储有关用户信息的一条数据,可以用来标识登录用户,Cookie存储在客户端的浏览器上.在大多数浏览器中,每个Cookie都存储为一个小文件.Cookie表示 ...

  8. Maven使用--基本入门

    maven学习(上)- 基本入门用法 转载自:https://www.cnblogs.com/yjmyzz/p/3495762.html 参考: http://www.cnblogs.com/dave ...

  9. 算法入门 - 链表的实现及应用(Java版本)

    之前我们学习了动态数组,虽然比原始数组的功能强大了不少,但还不是完全纯动态的(基于静态数组实现的).这回要讲的链表则是正儿八经的动态结构,是一种非常灵活的数据结构. 链表的基本结构 链表由一系列单一的 ...

  10. 刷题-力扣-1011. 在 D 天内送达包裹的能力

    1011. 在 D 天内送达包裹的能力 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/capacity-to-ship-packag ...