HDU 2647 Reward(拓扑排序)
来源:互联网 发布:python爬虫源码 编辑:程序博客网 时间:2024/06/01 08:13
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.
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
2 11 22 21 22 1
1777-1
题意:
老板要给每个员工发工资,每个员工的工资至少要有888,但是有的员工提出了别的要求,每一行要求的a,b表示:a的工资要比b的工资高。根据那些要求算出老板所要支付的最少工资。
因为数据上w,所以邻接矩阵空间不够,用了邻接表,同时拓扑排序的时候,记得反向建表。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int mx = 21000;int head[mx], cnt, value[mx], in[mx];int n, m;struct node{int u, v, next;} edge[mx];void add(int u, int v){edge[cnt].u = u;edge[cnt].v = v;edge[cnt].next = head[u];head[u] = cnt++;}void init(){cnt = 0;memset(head, -1, sizeof(head));memset(value, 0, sizeof(value));memset(in, 0, sizeof(in));}int topu(){queue<int>q;while(!q.empty()) q.pop();int co = 0, sum =0;for(int i = 1; i <= n; i++){if(in[i] == 0){q.push(i);co++;}}int fr;while(!q.empty()){fr = q.front();q.pop();for(int i = head[fr]; i != -1; i = edge[i].next){int u = edge[i].u, v = edge[i].v;in[v]--;if(in[v] == 0){ value[v] = value[u] + 1;sum += value[v];q.push(v); co++; }}}if(co == n) return sum + n * 888 ;else return -1;}int main(){int a, b;while(scanf("%d%d", &n,&m) != EOF){init();while(m--){scanf("%d%d", &b, &a);//反向建图 add(a, b);in[b]++;}printf("%d\n",topu());}return 0;}
阅读全文
0 0
- HDU 2647 Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- hdu 2647 Reward ( 拓扑排序 )
- HDU 2647 - Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- HDU 2647Reward(拓扑排序)
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 -- Reward (拓扑排序)
- hdu 2647 Reward(拓扑排序)
- HDU-2647 Reward(拓扑排序)
- HDU 2647Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- thinkPHP中使用foreach()遍历二维数组实例详解
- spring源码分析,重新认识spring五(内功心法 从思想上说明 spring 常用的几种设计模式,漫谈)
- jsp对dao层的工具类
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Notepad++ 外部样式表css文件引用不成功
- HDU 2647 Reward(拓扑排序)
- ViewPager图片无限轮播加轮播指示器
- Linux数字权限解释
- LeetCode 22. Generate Parentheses (Medium)
- Stanley Druckenmiller德鲁肯米勒
- 使用 Azure 门户创建 Linux 虚拟机
- 9.23/24周末作业
- 比特币早期投资家:没有人能够阻止其发展 TechWeb 09-27 09:10 凤凰科技讯 据CNBC网站北京时间9月27日报道,风险投资家、“Social+Capital”基金创始人Chamath
- linux软件的安装,更新与卸载