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;
}

最新文章

  1. 复习(1)【Maven】
  2. JS中setInterval与setTimeout的区别
  3. cocos2d-x之悦动的小球
  4. 快速反射DataTable
  5. hdu----(1847)Good Luck in CET-4 Everybody!(简单巴什博奕)
  6. 如何制作iso文件
  7. [RxJS] Reactive Programming - Sharing network requests with shareReplay()
  8. debian下安装repo
  9. java中的链表编写
  10. Android官方技术文档翻译——Apk 拆分机制
  11. Wireshark:couldn&#39;t run dumpcap in child process(附带Linux下探索过程)
  12. 基于socket构造c/s 架构软件
  13. postgresql-JSON使用
  14. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?
  15. InstallShield2015制作安装包----------卸载前结束执行中的进程
  16. 用户登录页面——jdbc
  17. MySQL 学习资料
  18. zuul超时及重试配置
  19. ES6学习笔记十一:编程风格技巧
  20. P3294 [SCOI2016]背单词

热门文章

  1. Unity初探—SpaceShoot
  2. leetcode-二进制手表
  3. post接口_ajax上传
  4. LeetCode 96——不同的二叉搜索树
  5. phantomjs抛出IOException
  6. BluetoothAdapter解析
  7. TCP系列15—重传—5、Linux中RTO的计算
  8. 【week4】课堂Scrum站立会议
  9. 基于c++和opencv底层的图像旋转
  10. matlab怎么选取excel的特定列构成数组