Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5617    Accepted Submission(s):
1707

Problem Description
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.
 
Input
One 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.
 
Output
For 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
 
唉!用优先队列WA了一下午,现在想想,人家题中也没说这种如果同样优先级谁放前边的话,而且根据题意同样优先级的人发的钱数应该一样
题解:发奖金,每个人至少888,但是一些工作做得好的觉得应该多拿,所以就排出了优先级,优先级高的要比优先级低的多拿,优先级相同的拿同样的钱,
        问最少发出去多少奖金;
题解:还是要用反向拓扑,因为所给的数据可能不只是一棵树,而是森林,
这里给出一组数据
3  2
1  2
1  3
结果是2665不是2666
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#define MAX 20010
using namespace std;
vector<int>map[MAX];
int vis[MAX];
int reward[MAX];
int n,m,sum;
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
map[b].push_back(a);
vis[a]++;
}
}
void tuopu()
{
int i,j,ok=0,sum=0;
queue<int>q;
memset(reward,0,sizeof(reward));
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
{
q.push(i);
reward[i]=888;
}
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
q.pop();
ans++;
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
{
q.push(v);
reward[v]=reward[u]+1;
}
}
}
if(ans!=n)
printf("-1\n");
else
{
for(i=1;i<=n;i++)
sum+=reward[i];
printf("%d\n",sum);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getmap();
tuopu();
}
return 0;
}

  

hdoj 2647 Reward【反向拓扑排序】的更多相关文章

  1. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  2. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  3. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  4. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. 杭电 2647 Reward (拓扑排序反着排)

    Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...

  6. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  7. CF-825E Minimal Labels 反向拓扑排序

    http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...

  8. 逃生(HDU4857 + 反向拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...

  9. HDU-4857-逃生-反向拓扑排序+优先队列

    HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...

随机推荐

  1. 找不好重现的bug的一个小技巧————守株待兔

    最近碰到一个问题就是数据库中偶尔出现一条没有id的数据,可恨的是怎么也找不到重现这个问题的原因,只好换种方式来找了,那么就是我标题所说的守株待兔方法. 因为我发现出现bug的数据库里面的数据有个字段为 ...

  2. Qt5对付中文真好用

    Qt好多C++程序员都在用,Qt4大家可能用的多,到了Qt5不熟悉的人到是很多,其中我喜欢的特性也是和Qt4大不一样的地方就是对中文的处理. Qt4中使用“QTextCodec::setCodecFo ...

  3. 用QT 还是MFC ? (转)

    我曾经使用过QT和MFC来开发过软件,我想和大家分享我使用他们时所体会的不同之处. 我并非一个职业作家,这篇文章可能看起来不如专业的杂志和网站上的那么条理清晰.但是,我在这里是用我自己的语言来表达我自 ...

  4. [CSS]background背景

    css背景样式 序号  中文说明  标记语法  1  背景颜色  {background-color:数值}  2  背景图片  {background-image: url('imgpath/img ...

  5. Python尾递归-求斐波那契数列

    # coding=utf-8 # Fibonacci.py Fib = {} def Fibonacci(n): global Fib if Fib.has_key(n): return Fib[n] ...

  6. python生成随机二进制文件

    import random def genFile(filename,block=1,size=1): f=open(filename,"wb") content="&q ...

  7. Apache添加虚拟主机目录

    <VirtualHost 127.0.0.2:80> DocumentRoot d:/abcd ServerName 127.0.0.2:80</VirtualHost> &l ...

  8. log4j源码阅读

    基于log4j1.2.17的源代码阅读 org.apache.log4j.xml.DOMConfigurator 类是log4j的xml配置文件初始化类 org.apache.log4j.Proper ...

  9. jsonp跨域问题记录

    这段时间用H5做移动app开发,遇到不少之前做web的时候不曾遇到的问题,记录一下,共勉-- 首先说一个:js跨域取数的问题 描述:  之前做web都是通过后台获取数据,没考虑过跨域的问题.这次用h5 ...

  10. [小知识]不显示没有内容的UITableViewCell

    开发过程中常常使用到UITableView,当tableView的内容不足一屏时,若设置了talbeView的高度为屏幕高度,就会出现没有内容的cell显示出来,效果非常不好看,要想让没有内容的cel ...