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
 
Sample Output
1777 -1
 
 
拓扑排序专题
vector 存图 注意vector的使用
算是拓扑排序的模板题目吧
多线程的  此题精妙在L数组 L初始为0

#include<bits/stdc++.h>
using namespace std;
vector<int> mp[20005];
int n,m;
int a,b;
int in[20005];
int L[20005];
int flag;
int re;
struct node
{
int sum;
};
queue<node>q;
struct node N,now;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
mp[i].clear();
memset(in,0,sizeof(in));
memset(L,0,sizeof(L));
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
mp[b].push_back(a);
in[a]++;
}
for(int i=1;i<=n;i++)
{
if(in[i]==0)
{
N.sum=i;
q.push(N);
}
}
int jishu=n;
int temp;
while(!q.empty())
{
now=q.front();
q.pop();
jishu--;
temp=L[now.sum];
//cout<<now.sum<<endl;
for(unsigned int i=0;i<mp[now.sum].size();i++)
{
if(--in[mp[now.sum][i]]==0)
{
N.sum=mp[now.sum][i];
L[N.sum]=temp+1;
q.push(N);
}
}
}
re=0;
if(jishu>0)
printf("-1\n");
else
{
for(int i=1;i<=n;i++)
re+=L[i];
printf("%d\n",re+888*n);
}
}
return 0;
}

HDU2647 topsort的更多相关文章

  1. HDU2647

    第一道逆拓扑纪念一下... #include<iostream> #include<cstdio> #include<cstring> #include<cm ...

  2. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

  3. POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)

    题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到 ...

  4. poj1094 topsort

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32275   Accepted: 11 ...

  5. POJ - 3249 Test for Job (DAG+topsort)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  6. 拓扑排序 topsort详解

    1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { marg ...

  7. poj 3648 2-SAT建图+topsort输出结果

    其实2-SAT类型题目的类型比较明确,基本模型差不多是对于n组对称的点,通过给出的限制条件建图连边,然后通过缩点和判断冲突来解决问题.要注意的是在topsort输出结果的时候,缩点后建图需要反向连边, ...

  8. 经典问题----拓扑排序(HDU2647)

    题目简介:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上, ...

  9. 拓扑排序基础 hdu1258,hdu2647

    由这两题可知拓扑排序是通过“小于”关系加边建图的 hdu2647 /* 拓扑排序的原则是把“小于”看成有向边 此题反向建图即可 并且开num数组来记录每个点的应该得到的权值 */ #include&l ...

随机推荐

  1. 前端开发工程师 - 06.Mini项目实战 - 项目简介

    第6章--Mini项目实战 项目简介 Mini项目简介-Ego社区开发 回顾: 页面制作 页面架构 JavaScript程序设计 DOM编程艺术 产品前端架构 实践课Mini项目--Ego: 主题:漫 ...

  2. 【聚合报告】- 秒懂jmeter

  3. Training Models

    In this page, I am going to talk about the 'hello world' model that is linear regression and train i ...

  4. sqlserver 2008 merger语句

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  5. 又见CLOSE_WAIT

    原文: http://mp.weixin.qq.com/s?__biz=MzI4MjA4ODU0Ng==&mid=402163560&idx=1&sn=5269044286ce ...

  6. redis 编译安装错误问题

    编译redis安装的时候报错如下: make[1]: [persist-settings] Error 2 (ignored) CC adlist.o/bin/sh: cc: command not ...

  7. sql update limit1

    更新限制:为了避免全表更新,错误更新影响太多,加上limit1 多了一层保障.

  8. oracle数据库之触发器

    触发器是许多关系数据库系统都提供的一项技术.在 ORACLE 系统里,触发器类似过程和函数,都有声明,执行和异常处理过程的 PL/SQL 块. 一. 触发器类型 触发器在数据库里以独立的对象存储,它与 ...

  9. eg_1

    1. 编写一个程序,输出一个字符串中的大写英文字母个数,小写英文字母个数以及非英文字母个数. 第一种方法: public class Test { public static void main(St ...

  10. Swift-闭包理解

    /* 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambda ...