Reward HDU - 2647
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.
InputOne 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.OutputFor 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 题意:输入两个数n,m。n表示公司人数,m表示不同人之间的关系。a b表示a的工资大于b.
题解:拓扑排序,找入度为0的点,所有的拓扑完没有剩余,说明没有环
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e4+;
const int mod = 1e9+;
int degree[maxn],add[maxn];
vector<int>G[maxn];
int n,m;
int toposort()
{
queue<int>que;
for(int i=;i<=n;i++)
if(degree[i] == )
que.push(i);
int ans = ;
while(que.size())
{
int ptr = que.front();
que.pop();
ans++;
for(int i=;i<G[ptr].size();i++)
{
degree[G[ptr][i]]--;
if(degree[G[ptr][i]] == )
{
que.push(G[ptr][i]);
add[G[ptr][i]] = add[ptr] + ;
}
}
}
if(ans != n)
return -;
int sum = ;
for(int i=;i<=n;i++)
sum += add[i] + ;
return sum;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(degree,,sizeof degree);
memset(add,,sizeof add);
for(int i=;i<=n;i++)
G[i].clear();
for(int i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[b].push_back(a);
degree[a]++;
}
printf("%d\n",toposort());
}
}
Reward HDU - 2647的更多相关文章
- 逆拓扑排序 Reward HDU - 2647
Reward HDU - 2647 题意:每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 样例1: 2 1 1 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- 题解报告: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 Reward Description Dandelion's uncle is a boss o ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 (拓扑排序 邻接表建图的模板) Reward
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward(图论-拓扑排序)
Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...
随机推荐
- 1像素border
1像素border 利用伪类和媒体查询: 伪类: border-1px($color) position:relative &:after display: block position: a ...
- HashWithIndifferentAccess
The params method returns the parameters passed to the action, such as those fromthe form or query p ...
- 【原创】SQL SERVER 2012安装配置说明(多图详解)
1. 优先安装软件 1. net framework3.5. 2. 在安装SQL SERVER 2012前需要3.5的支持.在WIN 2012系统可以在系统管理的添加角色和功能中安装,如下将[.NET ...
- April 8 2017 Week 14 Saturday
Life is the art of drawing without an eraser. 人生如画,落笔无悔. Yesterday I watched a film from Japan, Afte ...
- CRM WebClient UI和Hybris里工作中心跳转的url生成逻辑
CRM WebClient UI 把Work center的navigation target在client side不可见:在Chrome development tool里看不到,而是点击了Wor ...
- 画X,模拟水题
题目链接:http://codeforces.com/contest/404/problem/A #include <stdio.h> #include <string.h> ...
- 贪心,POJ(2709)
题目链接:http://poj.org/problem?id=2709 解题报告: #include <stdio.h> #include <algorithm> #inclu ...
- 2018.7.31 oracle rownum的理解
一.Rownum的描述: rownum是一个伪列,数据库中并不保存rownum的列值,它是oracle系统为返回的结果集顺序分配的行编号,rownum是随着结果集生成的,一旦生成,在同一个结果集中就不 ...
- 2017.11.5 Java Web ----案例:数据库访问JavaBean的设计
(12)案例----数据库访问JavaBean的设计 例题:数据库操作在一个Web应用程序中的后台处理中占有大比重,设计一组JavaBean封装数据库的基本操作供上层模块调用,提高程序的可移植性. [ ...
- c# 语法 字符串内插
结果截屏 参考文章 https://www.cnblogs.com/csproj/p/Interpolated_Strings.html c# 6.0语法 https://www.cnblogs.co ...