Reward HDU
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2647 Accepted Submission(s): 768
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.
1 2
2 2
1 2
2 1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std; const int N = 10005;
struct edge
{
int w;
edge *next;
}*e[N]; int cnt;
int deg[N], money[N];
int n, m; void init() //初始化
{
cnt = 0;
for(int i = 0; i <= n; i++)
{
e[i] = NULL;
money[i] = 888;
deg[i] = 0;
}
} void add(int x, int y) //建边
{
edge *p = (edge *)malloc(sizeof(edge));
p->w = y;
p->next = e[x];
e[x] = p;
} int main()
{
int i, x, y, k, u, num, sum;
while(scanf("%d %d", &n, &m) != EOF)
{
init();
sum = 0;
num = n;
for(i = 1; i <= m; i++)
{
scanf("%d %d", &x, &y);
add(y, x); //建边
deg[x]++; //度数++
}
queue<int> Q;
for(i = 1; i <= n; i++)
{
if(deg[i] == 0) Q.push(i);
}
while(!Q.empty())
{
k = Q.front();
Q.pop();
num--;
for(edge *p = e[k]; p; p = p->next) //链表代替矩阵
{
u = p->w;
if(--deg[u] == 0) //和k连接的点度数--,若为0,入队列
{
money[u] = money[k] + 1; //钱比连接点k的钱多1
Q.push(u);
}
}
}
if(num > 1) printf("-1\n"); //入队列次数少于n,证明有环
else
{
for(i = 1; i <= n; i++)
{
sum += money[i];
}
printf("%d\n", sum);
}
} return 0;
}
Reward HDU的更多相关文章
- 逆拓扑排序 Reward HDU - 2647
Reward HDU - 2647 题意:每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 样例1: 2 1 1 ...
- (回文串 )Best Reward -- hdu -- 3613
http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) ...
- Reward HDU - 2647
传送门 Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...
- Best Reward HDU 3613(回文子串Manacher)
题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值. 分析:首先的明白这个最大价值有 ...
- Best Reward HDU - 3613(马拉车+枚举+前缀和)
题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...
- 2019 HZNU Winter Training Day 14 Comprehensive Training
A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...
- POJ 3376 Finding Palindromes EX-KMP+字典树
题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
随机推荐
- winPcap_2_编译环境*注意*
使用WinPcap编程 创建一个使用 wpcap.dll 的应用程序 用 Microsoft Visual C++ 创建一个使用 wpcap.dll 的应用程序,需要按以下步骤: 在每一个使用了库的源 ...
- js中的function
Math方法详解 Math.sqrt(x) 计算X开平方 Math.sqrt(x,y) 计算xy Math.round(x) 计算x 四舍五入的值 getBoundingClientRe ...
- jQuery输入框提示自动完成插件 autocomplete
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- jquery navi
/// <reference path="../global.js" /> /********************************************* ...
- jdk,j2ee,j2se,j2me的概念区别
jdk,j2ee,j2se,j2me的概念区别1.JDK是Java development toolkit,相当于是Java的库函数,是编译,运行java程序的工具包.J2EE是Java 2 ente ...
- ibatis 更改resultmap后 java.sql.SQLException: Column 'del_status' not found.
当在resultmap中增加字段后,查询语句也必须增加相应字段,否则会报错, java.sql.SQLException: Column 'del_status' not found. 因为查询结果与 ...
- 异常处理:你不可能总是对的2 - 零基础入门学习Python033
异常处理:你不可能总是对的2 让编程改变世界 Change the world by program 我们已经了解足够多的可能碰到的异常,那我们这节课就来谈谈如何检测这些异常并处理它们. 异常检测我们 ...
- IOS 文件管理 2
IOS开发-文件管理(二) 五.Plist文件 String方式添加 NSString *path = [NSHomeDirectory( ) stringByAppen ...
- c# 输出excel、word实例只需一行代码
第一.首先下载 WindICFrameNet.dll 下载地址:http://pan.baidu.com/s/1dDBqC9r 第二.前台代码 <div style="display: ...
- NSScanner-备
注意:在扫描的时候,如果 空格是不需要扫描的,那么将会忽略空格. 如下代码: 1 NSString *string = @"my age is d 23 34.0"; ...