hdu 2647 Reward(拓扑排序,反着来)
Reward
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 51 Accepted Submission(s) : 21
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int i,n,m;
int a[],cnt[];
vector<int> s[];
long long sum;
bool toposort()
{
queue<int> Q;
int num=;
for(int i=;i<=n;i++)
if (cnt[i]==) {Q.push(i); a[i]=;}
while(!Q.empty())
{
int u=Q.front();
num++;
Q.pop();
for(int i=;i<s[u].size();i++)
{
cnt[s[u][i]]--;
if (cnt[s[u][i]]==)
{
Q.push(s[u][i]);
a[s[u][i]]=max(a[u]+,a[s[u][i]]);
}
}
}
if (num<n) return ;
return ;
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
for(i=;i<=n;i++) s[i].clear();
memset(cnt,,sizeof(cnt));
for(i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&y,&x);
s[x].push_back(y);
cnt[y]++;
}
memset(a,,sizeof(a));
if (!toposort())
{
printf("-1\n");
continue;
}
sum=;
for(i=;i<=n;i++) sum+=a[i];
printf("%lld\n",sum);
}
return ;
}
hdu 2647 Reward(拓扑排序,反着来)的更多相关文章
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 2647 Reward
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...
随机推荐
- tomcat配置多个虚拟主机
先修改默认端口(8080),http的默认端口是80,我们将8080改成80,这样域名就不用带上端口了 修改conf文件夹下的server.xml文件 添加多个主机(以下三个主机对应三个域名,三个域名 ...
- 关于oracle数据库(7)查询1
查询所有列数据 select * from 表名; 查询指定列数据 效率高于查询所有列数据 select 列名,列名,列名 from 表名; --先执行from后面的代码,找到表,在执行select后 ...
- awk中{print $1}什么意思
给你举个例子,echo "aa bb cc" | awk -F '{print $1}' 结果就是aa,意思是把字符串按空格分割,取第一个,自己做个测试就明白了!awk是用来提取列 ...
- python模拟登录知乎
# -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import sys from bs4 import Beau ...
- CSS3的box-sizing属性
盒模型的宽度,在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 border 与 padding 都包含在 width 之内 W3C标准中的盒模型宽度为内容宽度,不包括内边距paddin ...
- mybatis框架源码学习
转:来自https://my.oschina.net/u/1458864/blog/293659 摘要:初始化mybatis,所有的配置都在configuation 对象中使用mybatis,从sql ...
- 【bug】Unable to execute dex: Multiple dex files define
This is a build path issue. Make sure your bin folder is not included in your build path. Right clic ...
- PAT (Top Level) Practise 1005 Programming Pattern (35)
后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...
- c语言_头文件_stdlib
简介 stdlib 头文件即standard library标准库头文件 stdlib 头文件里包含了C.C++语言的最常用的系统函数 该文件包含了C语言标准库函数的定义 stdlib.h里面定义了五 ...
- 关于Unicode字符集
最初的unicode编码是固定长度的,16位,也就是2两个字节代表一个字符,这样一共可以表示65536个字符.显然,这样要表示各种语言中所有的字符是远远不够的.Unicode4.0规范考虑到了这种情况 ...