博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU Problem 2647 Reward【拓扑排序】
阅读量:6428 次
发布时间:2019-06-23

本文共 2524 字,大约阅读时间需要 8 分钟。

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7391    Accepted Submission(s): 2317

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
 

 

Author
dandelion
 

 

思路:将图倒着存,如果找到一个节点的上一级,就用res数组保存这个节点上级所处的等级,最后做一个统计。

#include 
#define MAXN 10005using namespace std;int n, m, outdegree[MAXN], res[MAXN];int head[MAXN], num, que[MAXN];struct node{ int from, to, next;}edge[MAXN*2];void add(int x, int y) { edge[num].from = x; edge[num].to = y; edge[num].next = head[x]; head[x] = num++;}void init() { memset(head, -1, sizeof(head)); memset(outdegree, 0, sizeof(outdegree)); memset(res, 0, sizeof(res)); num = 0;}void solved() { int iq = 0, ans = 0; for (int i = 1; i <= n; i++) { if (outdegree[i] == 0) { que[iq++] = i; } } for (int i = 0; i < iq; i++) { for (int k = head[que[i]]; k != -1; k = edge[k].next) { outdegree[edge[k].to]--; if (outdegree[edge[k].to] == 0) { que[iq++] = edge[k].to; //记录该节点所在的等级 res[edge[k].to] = res[edge[k].from] + 1; } } } for (int i = 1; i <= n; i++) { ans += res[i]; //如果存在环 if (outdegree[i] > 0) { printf("-1\n"); return ; } } printf("%d\n", ans + 888*n);}int main() { int a, b; while (scanf("%d%d", &n, &m) != EOF) { init(); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); add(b, a); outdegree[a]++; } solved(); } return 0;}

 

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770858.html

你可能感兴趣的文章
ios 代码创建UIToolBar
查看>>
病毒测试网站
查看>>
英语句子收集
查看>>
如何删除xcode中的多余证书
查看>>
linux网桥实现读后感
查看>>
h2启动脚本
查看>>
MySQL group_concat函数详解
查看>>
jquery:点击回到顶部以及定点滚动
查看>>
mysql如何更新一个表中的某个字段值等于另一个表的某个字段值
查看>>
Docker容器学习梳理--容器登陆方法梳理(attach、exec、nsenter)
查看>>
Android系统电量指示灯 Cubietruck
查看>>
PHP预定义常量
查看>>
IOS 如何选择delegate、notification、KVO?
查看>>
ng-app & ng-controller
查看>>
heartbeat+nginxProxy
查看>>
Cocos2d-x手机游戏开发与项目实战详解
查看>>
编程实现Linux流量监控
查看>>
Entity Framwwork Code First
查看>>
XBPageCurl
查看>>
jquery 闭包函数
查看>>