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: 给你一个字符串,每个字符都有一个权 ...
随机推荐
- 在Eclipse中运行hadoop程序
1.下载hadoop-eclipse-plugin-1.2.1.jar,并将之复制到eclipse/plugins下. 2.打开map-reduce视图 在eclipse中,打开window--> ...
- js过滤emoji表情符号
手机端常常会遇到用户输入框,输入emoji,如果是数据库是UTF8,会遇到报错:SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for ...
- 写下你的第一个Django应用,第三部分
这篇指南开始于指南2结束的地方.我们将继续web投票应用和集中注意力在创建公共接口——“view” 理念 一个视图在你的Django应用中一个web页面的“品种”和它通常作为一个特定的函数以及有一个特 ...
- EF查询
public ActionResult AllSettings(DataSourceRequest command, Framework.Kendoui.Filter filter = null, S ...
- libusb-win32 在visual studio2008中成功编译回忆录
关于这个项目不用多说 介绍 libusb是一个针对usb通讯的库. 使用它, 你不需要知道操作系统的细节, 你只需要对USB有足够的了解即可. 它也不需要你写驱动, 所有的工作都可以在用户态完成. 使 ...
- Deploy a Sharded Cluster
Start the Config Server Database Instances for example : mongod --configsvr --dbpath <path> - ...
- Java通过JNI调用C/C++
From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...
- Java vs Python
面试时常问到这两种语言的区别,在此总结一下. Referrence: Udemy:python-vs-java Generally, Python is much simpler to use, an ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- Highcharts 非常实用的Javascript统计图
Highcharts 官网: http://www.highcharts.com Highcharts 官网示例:http://www.highcharts.com/demo/ Highcharts ...